Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of the c code

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?

like image 775
webkul Avatar asked Nov 09 '09 07:11

webkul


People also ask

Is C code faster than Java?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

Why is C code fast?

But to answer your question, well-written C code will generally run faster than well-written code in other languages because part of writing C code "well" includes doing manual optimizations at a near-machine level.

Is C faster or Python?

C is a faster language compared to Python as it is compiled. Python programs are usually slower than C programs as they are interpreted. In C, the type of the various variables must be declared when they are created, and only values of those particular types must be assigned to them.


1 Answers

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 srandom() / 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.
like image 150
Roman Nikitchenko Avatar answered Sep 22 '22 09:09

Roman Nikitchenko