in c ++ 11, I believe the php microtime(true) equivalent 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)); }
- but I'm not sure what the C equivalent is, or if there is one at all (as others have already noted, gettimeofday is part of posix, but not part of the c / c ++ specification)
Interestingly, a microsecond is one millionth of a second, but c ++ also supports nanoseconds, which is one billionth of a second, I think you will get higher precision with std::chrono::nanoseconds instead of std::chrono::microseconds , but this the moment you are likely to encounter a maximum number limit equal to double , and the function name will be misleading (such a function should have the name nanotime() not microtime() , and the return value should probably be more than double), by the way I have a collection of php functions ported to c ++ here: https://github.com/di vinity76 / phpcpp (and among them microtime ())
hanshenrik
source share