4

Possible Duplicate:
How can I get the execution time of a program in milliseconds in C?
Calculating time of execution with time() function

I tried clock() in time.h but it always gives me 0.0000 seconds ie 0 seconds as output. Is there any way to get execution time in micro or Milli seconds or in any other smaller units?

Community
  • 1
  • 1
Yedhu Sastri
  • 51
  • 1
  • 4

3 Answers3

1

Precede the execution of your program in shell with "time", i.e.:

user@linux:~$ time c_program_name

Running the following, for example:

sampson-chen@linux:~/src/reviewboard$ time ls -R

Gives the following time results:

real    0m0.046s
user    0m0.008s
sys     0m0.012s

See the manual for time to adjust the display formats / precision / verbosity.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0

clock should work, you need to define it at the very beginning and print the value at last, check this out: http://www.cplusplus.com/reference/clibrary/ctime/clock/

Simon Wang
  • 2,843
  • 1
  • 16
  • 32
0

Clock displays 0.000 all the time because the execution is very fast and execution time is negligible. Try checking out time with some complex algorithms like Tower of Hanoi or NQueesns with big values. Then you'll get the time of execution in some milliseconds. I tried it for Tower of hanoi with 15 discs and it gave me some value for execution.

Afaq
  • 1,146
  • 1
  • 13
  • 25