Some loading routines in my program takes to long to complete. I want a quick small snippet for checking how long a function took to execute. By small I mean "preferably without 3rd party libraries".
Maybe something as simple as taking the system time?
start = current_system_time()
load_something()
delta = current_system_time()-start
log_debug("load took "+delta)
Edit: Target OS in question is Windows.
This is a quick and dirty way to time a block of C/C++ code. You need to
#include <sys/time.h>
, which should be a standard header...This should give 1-2µs resolution on modern Linux systems (what OS are you using?), which means that it's not well suited to learning much for items taking of <10µs. However, you don't seem to be in that situation.
Update: Based on specified OS... Windows implementation of gettimeofday()
I use a class for this, its designed to measure the time taken to execute a function and write that to a uth-16le text file (I need to update this to use a new class i made for this... but nm).
Create a new instance at the top of a function, e.g. jProfiler(L"myFunction") and the cleanup at the end of the function will do the rest, if you want to be sure though new and delete it yourself. Its a bit overkill for a small test, but might help:
I have a benchmark.hpp header in my sweet.hpp library. It has two benchmark tools. The first is a simple manual start stop time taker
The other one is a bit more sophisticated. You write functions or block statements like this.
And in the end call
sweet::Benchmark::printResults();
to have the time spend and the number of calls printed.Edit: I added a function so you can call it like this.
Your answer: Yes
Caveat: That WON'T work in multihtreaded code or multiple core machines, you need a robust wall-clock timer. So I recommend you use omp's wallclock. OMP is included with VC and GCC, and most compilers and its a standard you don't need to worry about disappearing