views:

326

answers:

6

I'm using gcc for my c programmes. How can I check which method is faster (suppose that I write a code to swap two numbers and I rewrote the same code using bit operator), is there any tool in linux to check the time,performance and space?

+8  A: 

man gprof should help.

But remember, if you use profiler you should test large number of loops. And you should do it with caching effect counted so at least this should be performed on large enough memory area data with random (but the same) order. Use sranddom() / random() for this.

Minimal setup:

  • write 'test bed' which loops different methods through the same input (large enough loops).
  • compile / link your modules with GNU compiler -pg option.
  • run. You should obtain profile data file (gmon.out usually).
  • man gprof -> gprof [options] -> produce reports you need -> see what you really need.
Roman Nikitchenko
thanks roman but how can i use this tool im a newbie to gprof thanks
vipinsahu
I have experienced that the profiling code that is compiled into the executable can affect the measurements, so I would definitely run without profiling too, and just measure times.
Thomas Padron-McCarthy
Thomas, it is the question of test bed quality. For simple measurements you even can just run big number of loops and use 'time' command ;). But this NEVER get you HOW you spend your time.
Roman Nikitchenko
If all you want is to test one operation then that is HOW you spend your time. The question was to compare two operations where you do not need the full context. This is my interpretation of the question.
Peter Lindqvist
+6  A: 

In the simple case you describe i'd use this

$ vi test.c
$ make test
cc     test.c   -o test
$ time ./test

real    0m1.001s
user    0m0.001s
sys     0m0.000s

When comparing the full execution of several components i'd use the gprof method outlined by Roman.

It all depends on the situation. But 9 times out of 10 the time approach is sufficient to me. But i suppose when working with threads multiple processes and GUI code the situation would be different. That is however not my field of expertise.

There is a tool called Valgrind (wikipedia) that i recommend you too have a look at.

There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail.

Peter Lindqvist
+1 for Valgrind point. I've just needed something like this but for different purpose.Approach with 'time' could lead to high measurement errors. It includes at least time to start and stop child process which has very big variation on loaded system. So it has _very_ limited application. And requires even higher number of independent test loops.
Roman Nikitchenko
A: 

If you need to monitor time within your program, or in a test code or log it, you can simply use times() or clock() .

http://www.gnu.org/software/libc/manual/html%5Fnode/Processor-And-CPU-Time.html

Sandip Bhattacharya
+1  A: 

For finding the time taken by your code, you could use something like this:

  clock_t start, end;
  start = clock();

< your block(s) of code here>


  end = clock();
  printf("Time taken %lf\n", (double) (end-start) / CLOCKS_PER_SEC);

It gives the time taken in seconds. Uses time.h

For finding the memory consumptions, you can check the relevant field using 'top' when you run the process.

Amit
A: 

The simplest way to do such a query is this:

for (i = 100000000; --i >= 0; ){
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
    swap(&a, &b);
}

where swap is the function you want to time, this code runs it 10^9 times.

Then just time it with your watch. The number of seconds it takes translates to nanoseconds.

Mike Dunlavey
A: 

With a function as simple as a number swap, the easiest way to compare memory usage and speed might be to look at the code in a debugger. When you can see the optimized assembly alongside the original C, you should be able to count assembly operations for each (can't imagine a number swap being more than a handful of assembly lines) and get a good handle on which method is faster, and look at the number of registers involved in each function to decide which method uses more memory. You wouldn't want to do this on large functions, however.

bta