I have a simple math vector class with operators overloaded. I would like to get some timing results for my operators. I can easily time my +=, -=, *=, and /= by timing the following code:
Vector sum;
for(size_t i = 0; i<iter; ++i)
sum += RandVector();
cout << sum << endl;
Then I can subtract the time it takes to generate iter random vectors. In my tests, Vector is 3 dimensional, iter = 10,000,000.
I tried to do a similar thing with +,-,*,/:
Vector sum;
for(size_t i = 0; i<iter; ++i)
sum = sum + RandVector();
cout << sum << endl;
Then subtract the time it takes to generate iter random vectors and perform iter assignments, however this gives a "negative" time, leading me to believe either the compiler is optimizing the operation somehow, or something strange is going on.
I am using gcc-4.7.2 using -O3 on a Fedora Linux machine.
Here is my timing code:
clock_t start, k = clock();
do start = clock();
while(start == k);
F()();
clock_t end = clock();
double time = double(end-start)/double(CLOCKS_PER_SEC);
cout << time - time_iter_rand_v - time_iter_ass;
Here F is a function object which performs the code above. time_iter_rand_v is the time it takes to create iter random vectors and time_iter_ass is the time it took for iter assignment operations.
My question is then how to get accurate timing of just the operator+ function not any assignments or random vector generation?