Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test the performance of a C function?

Are there some good ways to know how a function performs in C? I would like to, for example compare my own function to a library function.

like image 566
Fred Avatar asked Nov 06 '09 14:11

Fred


People also ask

How do you test the performance of a Web application manually?

Generally, manual performance testing can be done by checking the number of open connections - active sessions, database connections, amount of CPU time, amount of memory being used etc. This is the amount of performance checking that could be done by a human.


4 Answers

You need high-resolution timers.

On Linux, gettimeofday() is a decent choice, it gives you microsecond resolution. On Windows, QueryPerformanceCounter() is typical. Make sure you run your function many times, to get stable readings.

Quick sample, for Linux:

struct timeval t0, t1;
unsigned int i;

gettimeofday(&t0, NULL);
for(i = 0; i < 100000; i++)
  function_to_measure();
gettimeofday(&t1, NULL);
printf("Did %u calls in %.2g seconds\n", i, t1.tv_sec - t0.tv_sec + 1E-6 * (t1.tv_usec - t0.tv_usec));

You would of course adjust the count (100,000) to match the performance of the function. It's best if the function really takes a while to run, otherwise the loop and/or the function call overhead might dominate.

like image 195
unwind Avatar answered Oct 23 '22 08:10

unwind


The open-source Callgrind profiler (for Linux) is a really awesome way to measure performance. Coupled with KCacheGrind, you get really great visualizations of where your time is spent.

Callgrind is part of Valgrind.

  • Art
like image 23
Art Clarke Avatar answered Oct 23 '22 08:10

Art Clarke


Hello i will give you an example and explain it:

#include <stdio.h>
#include <time.h>

int main(void)
{

    clock_t start_clk = clock();

    /*
        put any code here
    */

    printf("Processor time used by program: %lg sec.\n", \
    (clock() - start_clk) / (long double) CLOCKS_PER_SEC);

    return 0;
}

output: Processor time used by program: 4.94066e-324 sec.

time.h:

declares clock_t which is an arithmetic(you can do math on this value like i do in the example) time value. basically put any code where the comment is.

CLOCKS_PER_SEC is a macro declared in time.h, use it as the denominator to convert the value into seconds.

it is essential to cast it to long double for two reasons:

  1. we don't know what type clock_t actually is, but we want to print it (what conversion will you put in printf?).
  2. long double is a very precise type which can represent really small values.
like image 25
NEMKA Avatar answered Oct 23 '22 07:10

NEMKA


Run it (them) several million times (each) and measure the time it takes.
The one that completes faster is the better performant.

gprof can help :)

Here's the result of gprof when I run a program of mine for 10 seconds (function names changed)

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 60.29      8.68     8.68 115471546     0.00     0.00  workalot
 39.22     14.32     5.64       46   122.70   311.32  work_b
  0.49     14.39     0.07                             inlined
  0.07     14.40     0.01       46     0.22     0.22  work_c
  0.00     14.40     0.00      460     0.00     0.00  find_minimum
  0.00     14.40     0.00      460     0.00     0.00  feedback
  0.00     14.40     0.00       46     0.00     0.00  work_a
like image 3
pmg Avatar answered Oct 23 '22 08:10

pmg