I have a function that writes a file. I need to profile the function in with two parameters, so I can see the difference. (Do not tell me about Visual Studios Profiling tools, I want to do it by myself). I want to apply something like this:
double start = getTime();
myFunction("param1");
double request = getTime() - start;
printf_s("Request time: %f", request);
How can I do this? (what have above is just pseudo-code, I don't know the real functions names)
Use std::chrono
, here is an example that will work for your code:
#include <chrono>
int main()
{
using milli = std::chrono::milliseconds;
auto start = std::chrono::high_resolution_clock::now();
myFunction("param1");
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "myFunction() took "
<< std::chrono::duration_cast<milli>(finish - start).count()
<< " milliseconds\n";
}
You can use either:
CLOCKS_PER_SEC
in order to obtain the seconds (needs #include <ctime>
)WinAPI
you can use GetTickCount() and divide it by 1000 in order to obtain the seconds.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With