I am wondering if there is an equivalent to the PHP function microtime() in C and C++. I looked around but couldn't find a definitive answer.
Thanks!
I am wondering if there is an equivalent to the PHP function microtime() in C and C++. I looked around but couldn't find a definitive answer.
Thanks!
in c++11 i believe the equivalent to php's microtime(true) is:
#include <chrono>
double microtime(){
return (double(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
}
interestingly a microsecond is one millionth of a second, but c++ also supports nanoseconds, which is one BILLIONTH of a second, i guess you'd get higher precision with std::chrono::nanoseconds instead of std::chrono::microseconds, but at that point you'd probably run into max number limits of double, and the function name would be misleading (such a function should have the name nanotime() not microtime(), and the return should probably be something bigger than double), btw i have a collection of php-functions-ported-to-c++ here: https://github.com/divinity76/phpcpp (and microtime() is among them)