Pablo Acuña
Pablo Acuña

Reputation: 77

how to find out what part of my code is slowing my c++ program

I wrote 2 versions of my program, wich is an evolutionary algorithm in c++. The first version is procedural and works fine and very fast. The second version is completely OOP, and the program finds results, but is very very slow (like 10 times slower than the 1st version). Is there a way to maybe measure time of segments of code inside loops or something like that? Any advice or idea would help. Thanks in advance.

Upvotes: 3

Views: 4523

Answers (5)

Alexandru C.
Alexandru C.

Reputation: 3395

You can use the \callcap compiler flag in VS. You can read about it here.

Basically you can add this flag only for the .cpp file that you want to analyze, define the enter/exit functions, rebuild your app, and run it. I suggest you split the code you are trying to analyze (and suspecting of being slow) into functions, and then you can see which piece of code takes more time to execute.

It's a little more work, compared to an already available profiler, but it's worth giving it a try.

Upvotes: 0

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19114

For your particular case, I suggest downloading and using this tool: http://www.codersnotes.com/sleepy/

It is a very simple (but efficient) sampling profiler. Just launch your app with Ctrl+F5 (release) in Visual Studio, run this program (Very Sleepy), double click your exe name, wait, and you will see a detailed report with function names.

For the next level, if needed, use VTune.

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 181047

You need a profiler to find performance related issues in your program.

Depending on the Visual Studio edition, you have various levels of profiling support in your Visual Studio. If you're lucky enough to be at the Visual Studio Ultimate or Premium edition, you have very good profiling support built right in.

If you're on Visual Studio Express or Visual Studio Professional, there is sadly no profiling support built into Visual Studio, but you can use for example info at this link how to do it manually for free with those editions anyway.

Upvotes: 1

Michael
Michael

Reputation: 637

Use a profiler. If you're compiling with gcc, look up gprof, for example.

Upvotes: 0

barefootliam
barefootliam

Reputation: 619

Use a profiler. Which one is best depends on the platform/operating environment; e.g. with g++ you can use gprof, or if you don't want to recompile you can use oprofile, assuming Linux. On Solaris you could use dtrace. On other platforms, such as Windows or Mac, add the tag for your platform to the question...

Upvotes: 1

Related Questions