I need to profile some code running C++ on Linux. Can you guys recommend some profilers?
-
1You should add Linux and C++ tags. You will probably get a better response and range of opinions. – Duck Jul 22 '09 at 23:18
-
2Looks like a duplicate of http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux. – Michael Myers Jul 23 '09 at 15:54
-
eg: likwid, LLTng, oprofile, valgrind, vtune, gprof, perf, gperftools, pTop – Shan Feb 18 '14 at 17:28
-
See this question on slant: https://www.slant.co/improve/topics/1005 – ideasman42 Jun 27 '17 at 05:36
9 Answers
Use gprof.
Just compile with -pg
flag (I think (but am not sure) you have to turn of optimizations though.) and use gprof to analyze the gmon.out file that your executable will then produce.
eg:
gcc -pg -o whatever whatever.c
./whatever
gprof whatever gmon.out
Same thing with g++ and cpp.
-
33Profiling unoptimized code is a bit pointless, isn't it? Similarly, profiling code that has been heavily modified with -pg often misguides you into optimizing the wrong spots. – federal Jan 05 '12 at 15:44
-
1
-
thought valgrind was more for memory leak checking.. I am trying to see which functions are getting called etc – shergill Jul 22 '09 at 23:07
-
14
-
2Valgrind is simply a framework for building dynamic tools. Although, it's become synonymous with Memcheck, a tool built on Valgrind. Callgrind is a pretty good at profiler. – Falaina Jul 22 '09 at 23:10
-
Zoom from RotateRight ( http://www.rotateright.com ) is what I've been using. It has a butterfly view of functions and you can double-click any function to dive into source or asm code. Build with debugging information (-g) to see your source, but you should still build and profile optimized code.

- 193
- 1
- 4
-
1Just gave this program a shot, it's really quite nice! Currently my favorite profiler on Linux; however it's worth mentioning that it requires code to be built with `-fno-omit-frame-pointer` to effectively profile. – Nik Reiman Mar 20 '15 at 09:05
-
1The link seems dead. Does anyone know where (or if) it can be found elsewhere? – Simon F Jun 16 '17 at 12:07
I'm a fan of Oprofile. It involves installing a kernel module and has a bit of a learning curve to it, but it's fairly powerful and works very well for optimized programs/programs without debugging symbols.
Vtune is another very powerful profiler made by Intel. I believe the Linux version is free for Non-commercial software.
There is also the Valgrind suite of tools proposed by dfa. Callgrind would probably be what you're most interested in. Cachegrind(whose featureset is a subset of Callgrind's) and Massif are interesting as well, but I have no experience with the latter.

- 6,625
- 29
- 31
-
1
-
1Haha, true. I should probably not make that sound so easy :) It's certainly not as simple as "run program under it" as Vtune and Valgrind tools, but I feel you get used to it pretty quickly. – Falaina Jul 22 '09 at 23:20
-
-
Google also has a nice profiler as part of the google-perftools -- which are included in Debian / Ubuntu and possibly other distros.

- 1,664
- 16
- 21

- 360,940
- 56
- 644
- 725
Take a look at KCacheGrind which is a graphical frontend to valgrind and makes it really easy to use it.

- 59,775
- 49
- 126
- 179
Take a look at Sysprof. You distribution most likely has it available already.
Note that all of the mentioned profilers work best if your application is compiled with frame pointers. That is, you should use -fno-omit-frame-pointer on the gcc command line.

- 539
- 3
- 3

- 1
- 1

- 40,059
- 14
- 91
- 135
-
you'll just base your judgement about the bottleneck on 10 samples you collect manually, instead of 1000 samples collected by `prof`. – Dmitry Grigoryev Jan 05 '21 at 21:49
-
1@DmitryGrigoryev: Correct, and that actually tells you what you should fix. The statistical explanation is [*here*](https://scicomp.stackexchange.com/a/2719/1262). In fact, the first mistake people make is to think that they are looking for a "bottleneck" rather than perfectly good but wasteful code ;-) – Mike Dunlavey Jan 06 '21 at 19:41
-
Nice read, thanks. I totally buy your point that optimizing is way easier when you see an actual function call in the debugger. And I do understand that "bottleneck" doesn't mean "good optimization target", just a potential one. Still, I think it makes sense to start with `prof` anyway: if I see that `f()` is the most problematic function statistically, I'll stop the program several times until I land in `f()` instead of just starting with a random function I stopped in first. – Dmitry Grigoryev Jan 06 '21 at 21:26