Thread: How to accuratle mesurament the performance of a function?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    105

    How to accuratle mesurament the performance of a function?

    Hello! So I'm trying to measurement the execution time of a function but I realized that I'm getting different results every time (for the same function). For example let's consider the following code snipped:

    Mesurament time is not accurate. ($2215987) * Snippets * Snippets * GitLab

    I have also tried with clock_gettime() and it gives me the same inaccurate results. However, what's weird is that If I try to do this more times, the seem to be close in times so only the first clock() will be inaccurate. Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    There are a multitude of reasons why.

    1. Your program is a userland process, so all your clocks are to some extent an approximation.

    2. calling malloc may involve a very expensive trap into the OS to get more memory. This seems quite likely on the very first call.
    But you don't know when the first call is. The C startup code before you get to main may also call malloc, so you really don't know.

    3. Repeated calling of the same code may benefit from the code already being in the instruction cache, as opposed to say main memory (or even on disk).

    So
    - try it with a user supplied buffer rather than calling malloc
    - call it many times in a loop, then take the whole elapsed time, divide by the number of loop iterations, to get an average.

    The only way you're getting instruction-accurate measurements is on a bare machine with no OS and no interrupts.
    The more stuff you have going on in the background, the fuzzier your results are going to be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    105
    Quote Originally Posted by Salem View Post
    There are a multitude of reasons why.

    1. Your program is a userland process, so all your clocks are to some extent an approximation.

    2. calling malloc may involve a very expensive trap into the OS to get more memory. This seems quite likely on the very first call.
    But you don't know when the first call is. The C startup code before you get to main may also call malloc, so you really don't know.

    3. Repeated calling of the same code may benefit from the code already being in the instruction cache, as opposed to say main memory (or even on disk).

    So
    - try it with a user supplied buffer rather than calling malloc
    - call it many times in a loop, then take the whole elapsed time, divide by the number of loop iterations, to get an average.

    The only way you're getting instruction-accurate measurements is on a bare machine with no OS and no interrupts.
    The more stuff you have going on in the background, the fuzzier your results are going to be.
    Seriously thanks a lot!!! A great explanation so I will know what to avoid in the future! Have an amazing day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STD and Performance
    By thetinman in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2018, 04:52 AM
  2. Function calls and performance
    By koplersky in forum C Programming
    Replies: 14
    Last Post: 12-11-2012, 08:55 PM
  3. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  4. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 03:09 PM
  5. Best performance.
    By Benzakhar in forum Game Programming
    Replies: 3
    Last Post: 01-14-2004, 10:34 AM

Tags for this Thread