3

I would like to know how I can program something so that my program runs as long as a second lasts.

I would like to evaluate parts of my code and see where the time is spend most so I am analyzing parts of it.

Here's the interesting part of my code :

int size = 256
clock_t start_benching = clock();
for (uint32_t i = 0;i < size; i+=4)
{
    myarray[i];
    myarray[i+1];
    myarray[i+2];
    myarray[i+3];
}
clock_t stop_benching = clock();

This just gives me how long the function needed to perform all the operations.

I want to run the code for one second and see how many operations have been done.

This is the line to print the time measurement:

printf("Walking through buffer took %f seconds\n", (double)(stop_benching - start_benching) / CLOCKS_PER_SEC);
ChronoTrigger
  • 8,459
  • 1
  • 36
  • 57
user3694354
  • 105
  • 8
  • An outer loop, that continues until the required time has past? – Some programmer dude Aug 19 '14 at 13:39
  • Side note: see [this question](http://stackoverflow.com/a/459704/2475084) for a more accurate benchmark. – Al.Sal Aug 19 '14 at 13:43
  • 1
    My advice would be to do the opposite, find a number of iterations that runs in around a second, then play with optimizations using the same number of iterations. The time functions are not very accurate, and so you'll have a certain amount of variation run to run. – dohashi Aug 19 '14 at 13:43
  • oh forgot the last line. edited and added – user3694354 Aug 19 '14 at 13:44
  • Note that the 1-second mark may occur, say, while calculating `i+2` prior to the third `myarray` lookup. Are you expecting it to abort the loop immediately (mid-statement), or finish running the current iteration (which takes additional time past the 1-second mark)? – Wyzard Aug 19 '14 at 13:52
  • 1
    If you just want to see where time is being spent in code you just use a profiler, eg gprof: http://sourceware.org/binutils/docs/gprof/index.html - don't make things complicated for yourself... – Spacedman Aug 19 '14 at 13:56
  • See [Can we force quit a C program's execution after a fraction of seconds](http://stackoverflow.com/questions/9223758/can-we-force-quit-a-c-programs-execution-after-a-fraction-of-seconds) for a number of solutions. – Jongware Aug 19 '14 at 14:09

2 Answers2

1

A better approach to benchmarking is to know the % of time spent on each section of the code. Instead of making your code run for exactly 1 second, make stop_benchmarking - start_benchmarking the total run time - Take the time spent on any part of the code and divide by the total runtime to get a value between 0 and 1. Multiply this value by 100 and you have the % of time consumed at that specific section.

1

Non-answer advice: Use an actual profiler to profile the performance of code sections.

On *nix you can set an alarm(2) with a signal handler that sets a global flag to indicate the elapsed time. The Windows API provides something similar with SetTimer.

#include <unistd.h>
#include <signal.h>

int time_elapsed = 0;

void alarm_handler(int signal) {
  time_elapsed = 1;
}

int main() {
  signal(SIGALRM, &alarm_handler);
  alarm(1); // set alarm time-out to 1 second

  do {
    // stuff...
  } while (!time_elapsed);

  return 0;
}

In more complicated cases you can use setitimer(2) instead of alarm(2), which lets you

  • use microsecond precision and
  • choose between counting
    • wall clock time,
    • user CPU time, or
    • user and system CPU time.
David Foerster
  • 1,461
  • 1
  • 14
  • 23