CacheSize and CacheStart Properties Example (Client/Server) (VC++)

This example moves through all records in a Recordset twice; once with no cache and once with a 50 record cache. The example then displays the performance statistics for the uncached and cached runs through the Recordset.

/*
CacheStart/CacheSize Properties:
This example moves through all records in a Recordset twice;
   once with no cache and once with a 50 record cache.
   The example then displays the performance statistics for
   the uncached and cached runs through the Recordset.
*/

#include      <stdio.h>
#include      <time.h>
#include      <afxole.h>
#include      <dbdao.h>
#include      <dbdaoerr.h>

CdbDBEngine      dben;
CdbDatabase       dbsPubs;
CdbWorkspace    wspDefault;
CdbTableDef       tdfPerformanceTest;
CdbRecordset      rstRemote;
CString         strNoCache, strCache;
time_t            tmStart, tmNoCache, tmCache;
COleVariant      vVal;
LPTSTR            szPurpose = 
                  _T("This example moves through ")
                  _T("all records in the Recordset ")
                  _T("twice;\n")
                  _T("once with no cache and ")
                  _T("once with a 50 record cache."),
               szAnnounce = _T(
                  "Caching Performance Results:\n"),
               szAnnounceNoCache = _T(
                  "Time without caching: %ld\n"),
               szAnnounceCache = _T(
                  "Time with 50 record cache: %ld\n");

wspDefault = dben.Workspaces[0L];
dbsPubs = wspDefault.OpenDatabase(_T("PUBLISH.mdb"));
tdfPerformanceTest =    dbsPubs.CreateTableDef(_T("TestData"));
tdfPerformanceTest.SetConnect(
  _T("ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers"));
tdfPerformanceTest.SetSourceTableName(
  _T("dbo.roysched"));
dbsPubs.TableDefs.Append(tdfPerformanceTest);
rstRemote = dbsPubs.OpenRecordset(_T("TestData"));

// Announce purpose of this test.
printf(szPurpose);

// Start uncached run.
tmStart = time(NULL);
for (int x = 0; x < 2; x++)
{
rstRemote.MoveFirst();
while (!rstRemote.EOF())
   {
   vVal = rstRemote.GetField(0L);
   rstRemote.MoveNext();
   }
}
tmNoCache = time(NULL) - tmStart;

// Start cached run.
rstRemote.SetCacheSize(50);
tmStart = time(NULL);
for (int x = 0; x < 2; x++)
{
rstRemote.MoveFirst();
rstRemote.SetCacheStart(rstRemote.GetBookmark());
rstRemote.FillCache();
while (!rstRemote.EOF())
   {
   vVal = rstRemote.GetField(0L);
   rstRemote.MoveNext();
   }
}
tmCache = time(NULL) - tmStart;

// Display performance results.
printf(szAnnounce);
printf(szAnnounceNoCache, (long)tmNoCache);
printf(szAnnounceCache, (long)tmCache);

rstRemote.Close();
dbsPubs.Close();