Microtime() Equivalent for C and C++?
Asked Answered
K

6

8

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!

Knop answered 22/7, 2012 at 22:2 Comment(0)
A
6

On Linux, you can use gettimeofday, which should give the same information. In fact, I believe that is the function that PHP uses under the covers.

Ayn answered 22/7, 2012 at 22:6 Comment(1)
and in C++11 you can std::chrono::system_clock::now().time_since_epoch()Paleography
A
9

There is no exact equivalent to PHP's microtime(), but you could a function with a similar functionality based on the following code:

Mac OS X and probably also Linux/Unix

#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL); #This actually returns a struct that has microsecond precision.
long microsec = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;

(based on: http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/)


Windows:

unsigned __int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
double timerFrequency = (1.0/freq);

unsigned __int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);

//do something...

unsigned __int64 endTime;
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
double timeDifferenceInMilliseconds = ((endTime-startTime) * timerFrequency);

(answer by Darcara, from: https://mcmap.net/q/1156398/-c-get-system-time-to-microsecond-accuracy-on-windows-duplicate)

Abjuration answered 16/8, 2012 at 12:19 Comment(4)
it's struct timeval time;Wyck
Note for Windows users: You have to include profileapi.h (#include <profileapi.h>).Goodhen
actually i disagree, as of C++11 there is an exact equivalent to PHP's microtime() - still, your answer may be the way to get it in C :)Paleography
Could someone explain the casts in the last row of the first example? I understand that it casts time.tv_sec to unsigned long long which will become the resulting type of the multiplication and also the addition. Then this gets assigned (and cast) into a long on the left. What is the idea here?Cassidycassie
A
6

On Linux, you can use gettimeofday, which should give the same information. In fact, I believe that is the function that PHP uses under the covers.

Ayn answered 22/7, 2012 at 22:6 Comment(1)
and in C++11 you can std::chrono::system_clock::now().time_since_epoch()Paleography
B
4

C++11 added some standard timekeeping functions (see section 20.11 "Time utilities") with good accuracy, but most compilers don't support those yet.

Mostly you need to use your OS API, such as gettimeofday for POSIX.

Babiche answered 22/7, 2012 at 22:6 Comment(0)
P
4

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));
}
  • but i'm not sure what the C equivalent is, or if there even is one (as others have pointed out, gettimeofday is part of posix, but not part of c/c++ specs)

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)

Paleography answered 6/4, 2019 at 21:33 Comment(0)
A
3

For timing sections of code, try std::clock, which returns ticks, then divide by CLOCKS_PER_SEC.

Alanalana answered 22/7, 2012 at 22:7 Comment(3)
I guess it depends on the platform: not necessarily on Windows, but other implementations should be XSI-compliant (CLOCKS_PER_SEC == 1000000)Alanalana
It's also not real time but cpu time.Wendelin
It also isn't an absolute timestamp (calendar), but simply elapsed time.Babiche
A
1

libUTP (uTorrent Transport Protocol library) has a good example on getting the microtime on different platforms.

Angleaangler answered 22/7, 2012 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.