0

I would like to know how long it takes to execute some code. The code I am executing deals with openCV matrices and operations. The code will be run in a ROS environment on Linux. I don't want the code to be interrupted by system functions during my benchmarking.

Looking at this post about benchmarking, the answerer said the granularity of the result is 15ms. I would like to do much better than that and so I was considering to make the function atomic (just for benchmarking purposes). I'm not sure if it is a good idea for a few reasons, primarily because I don't have a deep understanding of processor architecture.


void atomic_wrapper_function(const object& A, const object& B) {
  static unsigned long running_sum = 0;
  unsigned long before, after;
  before = GetTimeMs64();
  function_to_benchmark(A, B);
  after = GetTimeMs64();
  running_sum += (after - before);
}

The function I am trying to bench mark is not a short function.

  1. Will the result be accurate? For marking the time I'm considering to use this function by Andreas Bonini.

  2. Will it do something horrible to my computer? Call me superstitious but I think it's good to ask this question.


I'm using C++11 on the Linux Kernel.

Community
  • 1
  • 1
Klik
  • 1,757
  • 1
  • 21
  • 38
  • No sane compiler would optimize atomics – Guillaume Racicot Feb 10 '17 at 02:33
  • 2
    What exactly is meant by the word 'atomic' in this context? – Jeremy Friesner Feb 10 '17 at 07:26
  • Does "make the function atomic" mean something specific in OpenCV? Because I don't see anything atomic about that function, except its name. – Jonathan Wakely Feb 10 '17 at 11:57
  • 1
    "granularity of the result is 15ms. I would like to do much better" -- that's due to the [clock interrupt period](http://stackoverflow.com/a/3744104/3962537). If you want higher resolution, use [QPC](https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx) – Dan Mašek Feb 10 '17 at 12:48
  • The code was meant to help convey my idea, I don't know the specific syntax to do this yet. When I say atomic, I mean it in the context of RTOS (real time operating systems) where a task is uninterruptable--I know C++ has atomics but I may have jumped the gun and assumed it meant the same thing. Essentially I'd like to make a large function uninterruptable to get a more accurate time estimation of how much time my function adds to the larger project. – Klik Feb 10 '17 at 15:12
  • @DanMašek I should have specified that I am working on top of the Linux Kernel. – Klik Feb 10 '17 at 15:17
  • @Klik OK, just search around for high-res timers on Linux -- there's a lot of info around, even on SO. – Dan Mašek Feb 10 '17 at 15:25
  • Ok, but that still doesn't answer my question about using an atomic function. I'm also concerned with the function being interrupted during runtime by other processes. – Klik Feb 10 '17 at 15:26

1 Answers1

1

C++11 atomics are not atomic in the RTOS way, they just provide guarantees when writing multithreaded code. Linux is not an RTOS. Your code can and will always be interrupted. There are some ways to lessen the effects though, but not without diving very deeply into linux.

You can for example configure the niceness to get interrupted less by other userspace programs. You can tell the kernel on which CPU core to process interrupts, then pin your program to a different cpu. You can increase the timer precision etc, but:

There are many other things that might change the runtime of your algorithm like several layers of CPU caches, power saving features of your CPU, etc... If you are really only interested in benchmarking the execution time of your function for non-hard realtime problems, it is easier to just run the algorithm many many times and get a statistical estimate for the execution time.

  1. Call the function a billion times during the benchmark and average. OR
  2. Benchmark the function from 1 time to a billion times. The measure for execution time you are interested in should scale linearly. Then do some kind of linear regression to get an estimate of that.

OR: You say that you want to know what influence the algorithm has on your total program runtime? Use profiling tools like callgrind (integratable into QtCreator).

at-2500
  • 563
  • 4
  • 9