2 Replies - 8592 Views - Last Post: 20 November 2011 - 11:45 AM Rate Topic: -----

#1 theprofoundgeek   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Measuring execution time of a loop

Posted 20 November 2011 - 09:23 AM

Hello!

I have coded a compression technique and I am having trouble in measuring the speed of it. I tried to generate log of start time and end time, but compression time of same file varies a lot under windows. That may be happening because other processes would be consuming resources my program could me in blocked mode for a while. Can anyone please suggest me a way to measure the execution time of my program minus the waiting time when it was blocked?

I can think of three possible ways

(1) Setup a minimal Linux and run my program there so that no other process would interfere my program.

(2) Use some tool that would help me generate the report about the execution of my program.

(3) Code my program in such a way that it would tell me the absolute execution time at the end of compression.

Please guide me...

Is This A Good Question/Topic? 0
  • +

Replies To: Measuring execution time of a loop

#2 gundy_75   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 24
  • Joined: 10-November 09

Re: Measuring execution time of a loop

Posted 20 November 2011 - 10:21 AM

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ( void )
{
  clock_t start = clock();
  long i;

  for ( i = 0; i < 10000000; i++ )
    rand();

  printf ( "%f\n", ( (double)clock() - start ) / CLOCKS_PER_SEC );

  return 0;
}



The time.h header file does wonders. You can see there is a clock_t variable called start which calls a clock() function. Later, they call the clock function again, subtracting the start variable which stored the first clock() call.
Was This Post Helpful? 1
  • +
  • -

#3 theprofoundgeek   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Re: Measuring execution time of a loop

Posted 20 November 2011 - 11:45 AM

Thanks Gundy

That's a perfect solution for measuring start and end time and total execution time. But I need to measure absolute time. Let me give you a scenario :

  • My Program Starts execution
  • It does 4 iterations
  • it needs to wait for 2 minutes as its been blocked by the os
  • it does the rest of the job


Now this wait of 2 minutes needs to get deducted from the total execution time, as it was not the fault of the program. I hope I am explaining it properly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1