In my current project some part of code taking more than 30 minutes to complete the process. I found clock function would be best choice for getting the method execution time, but is there any other way to get the maximum time taking line of code? or else I have to log every method with clock function that would be a complex process for me because it is really gigantic project that would take forever to do it.
Asked
Active
Viewed 1,321 times
0
-
3*but is there any other way to get the maximum time taking line of code?* -- Yes, it's called a *profiler*. – PaulMcKenzie Aug 02 '19 at 13:58
-
1Welcome to Stackoverflow. What you are looking for is called 'profiling'. There is a lot of information about it. You could start here: https://stackoverflow.com/questions/2624667/whats-a-very-easy-c-profiler-vc – Bernd Elkemann Aug 02 '19 at 13:59
-
For starters, VS debugger shows each statement's ms on step. – Michael Chourdakis Aug 02 '19 at 14:00
-
I have used (and you should learn how to use) std::chrono. But my favorite function (for measuring durations) is the posix function, "int status = clock_gettime(CLOCK_REALTIME, &ts);" which accesses a system wide real time clock. "struct timespec ts" has factors to nanoseconds. You would measure 3 places ... start, approximate middle, end, splitting your code into 2 parts. Your slow function should be in either 1st part or 2nd part. Move your measurement points, and repeat. And yes, you could measure lots of durations and place in a vector. – 2785528 Aug 02 '19 at 18:09
-
I tried to run vtunes amplifier tool but it seems like only for .exe .I have a tool which runs to upload data to data base table and xx.dll(it's the dll which I have to improve performance) gets called when I run that tool .I checked the profiling status after finishing for bottle necks and it showing function address instead of function names and also even that function belong to tool instead of xx.dll fun. Is there tool which support dll profiling because current dll dependent of windows service. I am really new to profiling so I don't know how to handle this kind of situation. – Ramesh Nimmala Aug 02 '19 at 20:00
1 Answers
0
Proper way to do it - profiling. This will give you pretty useful information based on functions - where code spent most, which function was called most of the time etc. There are profilers that available on compiler level (like gcc has option to enable it) or you can use 3rd party ones. Unfortunately profiling itself will affect performance of the program and you may see different timing than real program with profiler enabled, but usually it is a good starting point.
As to measure execution time of every line of code, that is not practical. First of all not every line produces executable code especially after optimizer. On another side it is pretty useless to opimize code that not compiled with optimization enabled.

Slava
- 43,454
- 1
- 47
- 90