I am using this to time the operations of a
database engine I am building. such as load
a table, post, next, etc...
I just cant seem to use new and delete on it.
as the delete goes awry. I have solved some of
the usage problems by declaring
StopWatch sw;
as a global. This solves a lot of usage problems,
but the access violation seems to point to a problem
that should be fixed.
I have used new to create an instance of stopwatch
on a buttonclick and then immediately used delete.
Nothing else. then, bang all heck you know what.
it has been interesting though to watch the results.
if I instantiate the stopwatch object with just the
declaration as above and let the clas destroy itself
by falling through the function I discover that the
construction of this small object is very time consuming.
0.034 seconds as opposed to the quite small amount of
time (usually under a milisecond) to call small functions.
Code:
#include <ctime>
#include <stdio.h>
#include <string.h>
using namespace std;
const int strl = 100;
class __declspec(dllexport) StopWatch
{
public:
StopWatch (int Log = 0, char *str = "StopWatch :")
: FileName ("Stopwatch.log")
{
LogFile = Log;
if (strlen (str) < strl)
strcpy (string, str);
start = clock();
}
~StopWatch ();
double Stop ();
void Reset ();
void LeadString (const char *str);
private:
clock_t start;
int LogFile;
char string [strl];
const char *FileName;
};
Code:
#include "stopwatch.h"
using namespace std;
__declspec(dllexport) StopWatch::~StopWatch()
{
clock_t total = clock () - start; //get elapsed time
if (LogFile >= 2)
{
FILE *file;
file = fopen (FileName, "a");
fprintf (file, "%s: %s%.3f\n", string, "(destruct) total elapsed time in seconds: ", (total)/CLOCKS_PER_SEC);
fclose (file);
}
}
double __declspec(dllexport) StopWatch::Stop()
{
clock_t total = clock () - start; //get elapsed time
if ((LogFile == 1) || (LogFile == 3))
{
FILE *file;
file = fopen (FileName, "a");
fprintf (file, "%s: %s%.3f\n", string, "(stop) total elapsed time in seconds: ", (total)/CLOCKS_PER_SEC);
fclose (file);
}
return total/CLOCKS_PER_SEC;
}
void __declspec(dllexport) StopWatch::Reset ()
{
start = clock ();
}
void __declspec(dllexport) StopWatch::LeadString (const char *str)
{
strcpy (string, str);
}
as stated earlier this class is in a DLL
TC