How to measure running time? (per language)

I am looking for the best method to measure the running time of a code in each supported language. Yes, the answer is there in the 27 language documentations, but maybe the community could help me out much faster and better, please?

Something like this pseudocode:

start timer
// .. do stuff
get elapsed time in millisecs
print it to standard error stream formatted

For a start, here it is in PHP:

$startTime = microtime(TRUE);
// do stuff
$thinkTime = microtime(TRUE) - $startTime;
error_log('Think time = ' . number_format($thinkTime, 4, '.', '') . ' sec');

Note: microtime() can and shall be replaced with hrtime() as soon as CG moves up to PHP 7.4.

How to best do this in C#, C++, Java, Python, Haskell, etc? Please also add ‘import’ clauses if needed.

1 Like

I have to say any method i tried to measure time in JavaScript on CG didn’t seem to give reliable accurate results. I didn’t have these problems for other languages though: python, C++, java for example …

This was years ago, but i stopped trying because of it.

1 Like