Get time used by the process only - c++

I have a sample piece of code to check how much time it takes to execute. Hence I measure the timestamp before and after its execution and then compute the time in milliseconds.
However the output is dependent on system load and process priorities. As such I am not getting the correct reading.
How can I get the actual time spent by process only for its execution?
Platform Windows Compiler - VC and MinGW

Use Win32 function:
QueryProcessCycleTime()

Related

Will std::fopen execution time depend on file system content in Linux?

I am running profiling of my C++ code on a purpose built machine which is running Linux. The machine is re-installed sometimes, something which is outside of my control, but i'm thinking that the file system of the machine is gradually filled during the two weeks or so between the cleanups, and that this impacts my profiling measurements.
I have noticed that my profiling results get worse over time and then back to normal when the machine is cleaned up. This led to further investigation and i can see that std::fopen takes ~10 times longer to execute the day before cleanup compared to the day after.
Is it expected that std::fopen execution time is depending on what is stored in the file system? The file I'm opening is located in a directory which is always empty when i start my test case. Could there be some search involved regardless when running std::fopen or why would the execution time vary so much?
std::fopen after cleanup : 0.000098141 seconds
std::fopen before cleanup: 0.000940125 seconds
Running a decently new gcc. The machine has ARM architecture.

How can I make a function creating tens of thousands of symbolic filesystem links show up in VTUNE?

I'm profiling some binary on CENTOS 7.6 using VTUNE. I've yet to find the function (in vtune output) which is creating tens of thousands of symbolic file system links. And there is another one reading many such links, which I also cannot find. I used both,
basic hotspot analysis
locks and waits
Is the "basic hotspot analysis" only user-CPU-time but excludes system-CPU-time?
Where can one find the actually time spent (not CPU time) inside a function?
Hotspot analysis includes System-CPU time too. To be clear, the total time (real CPU time) is the combination of the amount of time the CPU spends executing a program and the amount of time the CPU spends performing system calls for the kernel on the program’s behalf.
Actual time spent inside a function: After running hotspots analysis, hover to the bottom-up tab in Vtune GUI to view the execution time for all functions in the user code. Double click on any of the function to view the execution time

Benchmarking Code Runtime with Trace32

I have an embedded system with code that I'd like to benchmark. In this case, there's a single line I want to know the time spent on (it's the creation of a new object that kicks off the rest of our application).
I'm able to open Trace->Chart->Symbols and see the time taken for the region selected with my cursor, but this is cumbersome and not as accurate as I'd like. I've also found Perf->Function Runtime, but I'm benchmarking the assignment of a new object, not of any particular function call (new is called in multiple places, not just the line of interest).
Is there a way to view the real-world time taken on a line of code with Trace32? Going further than a single line: would there be a way to easily benchmark the time between two breakpoints?
The solution by codehearts, which uses the RunTime commands, is just fine if you don't have a real-time trace. It works with any Lauterbach tool and any target CPU.
However if you have a real-time trace (e.g. CPU with ETM and Lauterbach PowerTrace hardare), I recommend to use the command Trace.STATistic.AddressDURation <start-addr> <end-addr> instead. This command opens a window which shows the average time between two addresses. You get best results, if you execute the code between the two addresses several times.
If you are using an ARM Cortex CPU, which supports cycle-accurate timing information (usually all Cortex-A, Cortex-R and Cortex-M7) you can improve the accuracy of the result dramatically by using the setting ETM.TImeMode.CycleAccurate (together with ETM.CLOCK <core-frequency>).
If you are using a Lauterbach CombiProbe or uTrace (and you can't use the ETM.TImeMode.CycleAccurate) I recommend the setting Trace.PortFilter.ON. (By default the port-filter is set to PACK, which allows to record more data and program flow, but with a slightly worse timing accuracy.)
Opening the Misc->Runtime window shows you the total time taken since "laststart." By setting a breakpoint on the first line of your code block and another after the last line, you can see the time taken from the first breakpoint to the second under the "actual" column.

I tried to come up with a cross platform alternative to sleep(), but my code isn't quite working

I'm doing a little beginner c++ program based on the game of snap.
When i output the card objects to the console, because of the computers processing speed naturally, a whole list of the cards that were dealt just appears. I thought it might be nice if i could put a pause between each card deal so that a human could actually observe each card being dealt. Since i'm always working on both Linux and Windows and already had < ctime > included i came up with this little solution:
for(;;){
if( (difftime(time(0),lastDealTime)) > 0.5f){ //half second passed
cout << currentCard <<endl;
lastDealTime = time(0);
break;
}
}
At first i thought it had worked but then when i tried to speed up the dealing process later i realised that changing the control value of 0.5 (i was aiming for a card deal every half a second) didn't seem to have any effect.. i tried changing it to deal every 0.05 seconds and it made no difference, cards still seemed to be output every second i would guess.
Any observations as to why this wouldn't be working? Thanks!
The resolution of time() is one second -- i.e., the return value is an integral number of seconds. You'll never see a difference less than a second.
usleep() is in the standard C library -- it has a resolution in microseconds, so use that instead.
time() and difftime() have a resolution of a second, to there's no
way to use them to manage intervals of less than a second; even for
intervals of a second, they're not usable, since the jitter may be up to
a second as well.
In this case, the solution is to define some sort of timer class, with a
system independent interface in the header file, but system dependent
source files; depending on the system, you compile one source file or
the other. Both Windows and Linux do have ways of managing time with
higher resolution.
If you want to make sure that the cards deal at precisely the interval you request, then you should probably create a timer class too. We use:
In Windows use QueryPerformanceFrequency to get the system tick time and QueryPerformanceCounter to get the ticks
On Mac Carbon use DurationToAbsolute to get system tick time and UpTime to get the ticks.
On Linux use clock_gettime.
For sleep use:
One Windows use Sleep();
On Mac Carbon use MPDelayUntil();
On Linux use nanosleep();
the big issue with your code from the way I see it is not the fact that you have not found a single-platform version of sleep but the fact that sleep is actually meant to stop the CPU from processing for a period of time, but yours will not stop processing and your application will use up lots of resources.
Of course if your computer is dedicated to just running one application it might not matter, but nowadays we expect our computers to be doing more than just one thing.

Accurate benchmark under windows

I am writing a program that needs to run a set of executables and find their execution times.
My first approach was just to run a process, start a timer and see the difference between the start time and the moment when process returns exit value.
Unfortunately, this program will not run on the dedicated machine so many other processes / threads can greatly change the execution time.
I would like to get time in milliseconds / clocks that actually was given to the process by OS. I hope that windows stores that information somewhere but I cannot find anything useful on the msdn.
Sure one solution is to run the process multiple times and calculate the avrage time, but I wont to avoid that.
Thanks.
You can take a look at GetProcessTimes API.
The "High-Performance Counter" might be what you're looking for.
I've used QueryPerformanceCounter/QueryPerformanceFrequency for high-resolution timing in stuff like 3D programming where the stock functionality just doesn't cut it.
You could also try the RDTSC x86 instruction.