Timing Your Code

You should always time your code to find out which parts of it are slow. This is an excellent application for test harnesses.

The Standard C library provides a time function for you to use. Unfortunately, it only measures time in one-second increments, so you will have to run many tests to get a time you can measure. For instance:

#include <time.h>

time_t Start;
time_t End;

time(&Start);
for (int Index = 0; Index < 100000; Index++)
{
   int Value = Accumulator.Value();
} ;

time(&end);

double TimeRequired = difftime(Start,End);

Keep in mind that some operations cannot be easily or reliably performed in a loop of this sort. For instance:

#include <time.h>

time_t Start;
time_t End;

time(&Start);

for (int Index = 0; Index < 100000; Index++)
{
   int Value = Accumulator.Apply(aRequest(aRequest::add,34));
} ;

time(&end);

double TimeRequired = difftime(Start,End);

This code applies 100,000 requests to the accumulator, which means that you might actually be testing the effects of a large tape rather than the speed of the calculation. Make sure that you carefully consider what you are testing.

In any event, properly gathered timings are the only way to pinpoint which parts of your program need optimization.

You can use timing test harnesses to find out whether your changes have actually succeeded in speeding up your program.

Also, don't forget that you can use a profiler to see where your program spends its time, as discussed in Lesson 23, “Object Testing Using Inheritance.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.131