Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ timing method with lower sys time
    text
    copied!<p>I'm trying to make my program self timed and I know two methods</p> <p>1) using getrusage</p> <pre><code>truct rusage startu; struct rusage endu; getrusage(RUSAGE_SELF, &amp;startu); //Do computation here getrusage(RUSAGE_SELF, &amp;endu); double start_sec = start.ru_utime.tv_sec + startu.ru_utime.tv_usec/1000000.0; double end_sec = endu.ru_utime.tv_sec + endu.ru_utime.tv_usec/1000000.0; double duration = end_sec - start_sec; </code></pre> <p>This fetches the user time of a program segment.</p> <p>2) using clock(), which gets the processor's executing time</p> <pre><code>double start_sec = (double)clock()/CLOCKS_PER_SEC; //Do computation here double end_sec = (double)clock()/CLOCKS_PER_SEC; double duration = end_sec - start_sec; </code></pre> <p>This fetches the real time of a program segment.</p> <p>However, I get really long sys time for both methods. The user time is also longer than without these timings. System time sometimes even doubles the user time.</p> <p>For example, I'm doing Traveling Salesman Problem, for a input that runs around 3 seconds for both user and real time normally, these two timings both make the user time to be over 5 seconds and real time over 15 secs, which means sys time is around 10 seconds long.</p> <p>I hope to know if there are ways of improvements or other libraries that are capable of shortening the sys time and user time if possible. If I have to user other libraries, I want libraries for both user time timing and real time timing.</p> <p>Thanks for any advice!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload