How much milliseconds to loop through a for loop?

Given a for loop:

1
2
3
int i;
for(i = 0; i < 1000; i++)
    ;



How many nanoseconds, or microseconds or milliseconds does it take for each iteration? And why do some use it as a timer mechanism?
How many nanoseconds, or microseconds or milliseconds does it take for each iteration?


It depends on the compiler, the compiler's settings, the hardware on the user's computer, how much scheduling time the program gets from the OS, and a dozen other factors. It does not translate cleanly to a length of time.

In this case... a smart compiler will optimize out that entire loop because it does nothing, and will merely do i = 1000;, which is a single mov instruction (about as fast as it can get)

And why do some use it as a timer mechanism?


They don't.

What they're actually doing is looping something a thousand or several thousand times and taking the average (mean) run time. Since each run can be of varying length (due to above mentioned factors)... taking the average gives you a better idea.

Plus it weeds out issues with timer resolution. If your timer can't time anything faster than 1 millisecond... then timing something that runs for 20 nanoseconds will appear to take as long as something running for 500 nanoseconds. So running for several thousand times will bring that number up to somewhere that's easier to measure.
Last edited on
Someone else posted this question a while back:

http://www.cplusplus.com/forum/unices/51209/

Based on what I see there, I think I am talking to a similar device as they are. I notice they use gettimeofday to count ellapsed microseconds until the difference reaches a max of var, in their example. Well, I was wondering why they were using gettimeofday and I am on linux too, so I created this test program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

unsigned long getTime()
{
  struct timeval detail_time;
  gettimeofday(&detail_time,NULL);
  return (detail_time.tv_usec); /* microseconds */
}

int main(void)
{
  int start,diff,end;
  unsigned long act_begin = getTime();
  start = getTime();
  
  do {
       diff = getTime() - start;
  } while(diff < 80);
  unsigned long act_end = getTime();
  printf("Actual begin: %lu Actual end: %lu\n", act_begin, act_end);
  return 0;
}


And I noticed the ellapsed time between actual begin and actual end is 735642 - 735558 = 84. That is 84 microseconds. It is interesting how the loop only goes 80 times and the length of time it took is 84 microseconds. It almost seems as if each iteration of loop is a around 1 microsecond. I am on Ubuntu 12.04, intel quad core cpu, 3 gb of RAM. And I am using 32-bit version of ubuntu. So I was just thinking probably they use gettimeofday here because it returns microseconds and they can use that to "wait" until around 80 microseconds passed. At least that is what their intention seems.
If you don't have to do a busy wait:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <thread>
#include <chrono>
#include <iostream>

int main()
{
    std::cout << "begin wait (800 microseconds)\n" << std::flush ;
    auto t1 = std::chrono::high_resolution_clock::now() ;
    
    // wait until at least 800 microseconds have passed.
    std::this_thread::sleep_for( std::chrono::microseconds(800) );
    
    auto t2 = std::chrono::high_resolution_clock::now();
    std::cout << "waited for " << std::fixed
              << std::chrono::duration_cast<std::chrono::nanoseconds>( (t2-t1) ).count() / 1000.0
              << " microseconds.\n\n" << std::flush ;
    
    std::cout << "begin wait (3.5 seconds)\n" << std::flush ;
    auto t3 = std::chrono::steady_clock::now() ;

    // wait until at least 3.5 seconds have passed.
    std::this_thread::sleep_for( std::chrono::milliseconds(3500) );
    
    auto t4 = std::chrono::steady_clock::now() ;
    std::cout << "waited for " << std::fixed
              << std::chrono::duration_cast<std::chrono::microseconds>( (t4-t3) ).count() / 1000000.0
              << " seconds.\n\n" << std::flush ;
}

http://coliru.stacked-crooked.com/a/5b6d39ce4132a0b4
Topic archived. No new replies allowed.