0

To calculate the total time taken for a c program we usually make use of functions as clock() and time() present in the header file <time.h> .
The clock() measures the number of ticks the CPU has used (CLOCKS_PER_SEC) after that we can easily divide the difference between the starting and the ending points of the code snippet we wish to calculate by CLOCKS_PER_SEC. This helps us in getting the time taken in sec
Also time(null) returns the number of whole seconds since a particular point in time. referred here

Now here is my problem. To calculate the total time taken (to document speed up operation) by a normal c program which when run using OpenMP, I make use of all the processors present in my system, what is the best approach to calculate the time taken. As clock() would just return the the cumulative time taken to run the program on each processor, basically

(actual_time_taken x omp_get_max_threads())

And time() is a very vague method to calculate as I am interested in calculation of time in milliseconds.

Community
  • 1
  • 1
pradyot
  • 174
  • 12
  • IMHO this is a duplicate of https://stackoverflow.com/questions/12392278/measure-time-in-linux-time-vs-clock-vs-getrusage-vs-clock-gettime-vs-gettimeof. My personal recommendation would be `clock_gettime(CLOCK_MONOTONIC_RAW ...` if available. Unfortunately there is no silver bullet - it is always a trade-off between the quality of the clock and portability and of course the requirements. – Zulan May 07 '16 at 06:55
  • 3
    I didn't see any reason for avoiding omp_get_wtime. If you have specific problems with timer quality due to your OS this should present a reasonable solution. For example, on Windows it might be based on queryperformance API. – tim18 May 07 '16 at 11:19

2 Answers2

5

OpenMP provides omp_get_wtime() for doing exactly this. Is there some reason not to use this function in your code?

jefflarkin
  • 1,279
  • 6
  • 14
-1

Since you are programming in C, you should use the function getrusage(). This will tell you the system and user mode times for either the entire process and its children, the current process alone, or all threads. Call it once at the start and once at the end, then take the difference.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm afraid this answer is wrong on many levels: 1) `TICKS_PER_SEC` has nothing to do with the CPU frequency. It is a header `#define` so it can't possibly refer to the frequency. Thus it's impossible for `clock` to use the time stamp counter (TSC) directly. 2) Modern CPUs have [constant TSC](https://en.m.wikipedia.org/wiki/Time_Stamp_Counter#Implementation_in_various_processors), so you can use TSC despite frequency scaling. – Zulan May 07 '16 at 06:43
  • It appears to be a simple misnomer with `CLOCKS_PER_SEC` which is defined as `1M` on modern CPUs and can be used with a difference of the double values read from `clock()` to determine the number of seconds of operation. – David C. Rankin May 07 '16 at 06:58
  • @Zulan: OK, I'd deleted that part of my answer and left the part which I'm pretty sure is still good advice. :) Thank you. – John Zwinck May 07 '16 at 13:51
  • Sorry, the advice is till wrong. getrusage() is returning CPU times. For speedup measurements you want elapsed (wall clock) times. The advice to use omp_get_wtime() is good. – Jim Cownie May 09 '16 at 11:31
  • Thank you for all these replies. I am working on Linux OS (ubuntu 14.04) with cpu Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz. omp_get_wtime() returns the time in seconds, is there no such funtion which returns in milli seconds? – pradyot May 10 '16 at 04:19
  • @pradyot: `omp_get_wtime()` returns the number of seconds as a double. Does it not give you fractional seconds? Or are you just truncating the fraction by mistake? Try multiplying by 1000 to get milliseconds. – John Zwinck May 10 '16 at 04:22
  • oh yes got it!! .. 'omp_get_wtime() ' solved my problem. Thanks a lot – pradyot May 10 '16 at 04:32