Calculating the runtime of my C program?

Possible duplicate:
How to get program runtime in milliseconds in C?


Calculating runtime using the time function ()

I tried clock() in time.h , but always gives me 0.0000 seconds, i.e. 0 seconds as output. Is there a way to get the runtime in micro- or milliseconds or in any other smaller units?

+4
source share
3 answers

Embark on executing your program in a shell with "time", that is:

 user@linux :~$ time c_program_name 

Running the following, for example:

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

It gives the following results:

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

See the time guide for adjusting display / accuracy / detail formats.

+1
source

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

0
source

The clock displays 0.000 all the time because the execution is very fast and the execution time is negligible. Try checking the time with some sophisticated algorithms such as Tower of Hanoi or NQueesns with large values. Then you will get the runtime in milliseconds. I tried this for the Tower of hanoi with 15 discs and it gave me some performance value.

0
source

Source: https://habr.com/ru/post/1440206/


All Articles