mreff555
mreff555

Reputation: 1111

Successive clock() operations over identical intervals have incremental consumption. Why?

While utilizing the clock() function in time.h I noticed some behavior I could not explain. In the code below I am using identical for loops to eat up processing time. I don't see any logical reason why one would consistently take longer than the other, but based on what I am seeing with the clock() function it would seem that each iteration takes roughly 7 - 10% longer. What is the reasoning behind this?

I added a typical representation of the output. The compiled ASM is over 1300 lines of code. I didn't think it would be constructive to post, but if you need to see it I can e-mail it to you.

output:

a: 3835
b: 4155
Percentage increase in execution time: 8%
c: 4423
Percentage increase in execution time: 15%
d: 4699
Percentage increase in execution time: 22%
e: 4976
Percentage increase in execution time: 29%

source:

#include <ctime>
#include <iostream>

int main()
{
  clock_t a, b, c, d, e;
  unsigned int aResult, bResult, cResult, dResult, eResult;

  a = clock();
  for(int i = 0; i < 100000; i++);
  aResult = a;
  std::cout << "a: " << aResult << std::endl;

  b = clock();
  bResult = b;
  for(int i = 0; i < 100000; i++);
  std::cout << "b: " << bResult << std::endl;
  std::cout << "Percentage increase in execution time: " 
    << 100 * (bResult - aResult)/aResult << "%" <<std::endl;

  c = clock();
  for(int i = 0; i < 100000; i++);
  cResult = c;
  std::cout << "c: " << cResult << std::endl;
  std::cout << "Percentage increase in execution time: " 
    << 100 * (cResult - aResult)/aResult << "%" <<std::endl;

  d = clock();
  for(int i = 0; i < 100000; i++);
  dResult = d;
  std::cout << "d: " << dResult << std::endl;
  std::cout << "Percentage increase in execution time: " 
    << 100 * (dResult - aResult)/aResult << "%" <<std::endl;

  e = clock();
  for(int i = 0; i < 100000; i++);
  eResult = e;
  std::cout << "e: " << eResult << std::endl;
  std::cout << "Percentage increase in execution time: " 
    << 100 * (eResult - aResult)/aResult << "%" <<std::endl;
}

Upvotes: 0

Views: 71

Answers (1)

Steven W. Klassen
Steven W. Klassen

Reputation: 1571

The problem you are having is that the statistics you are printing are incorrect. (bResult - aResult)/aResult and so are on are not telling you the percentage of increase in the time intervals, as aResult is the time since the start of the program - not just the time of the for loop. Furthermore (dResult - aResult)/aResult and the others are always comparing your latest result to the first result when you need to compare it to the previous result.

If you change your expressions to be the form ((eResult - dResult) - (bResult - aResult))/(bResult - aResult) you should get a more consistent answer. That is, at each iteration you need to compare the most recent interval (eResult - dResult) with the first interval (bResult - aResult) and not the first time.

Upvotes: 1

Related Questions