Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get system time in C++?

Tags:

c++

In fact i am trying to calculate the time a function takes to complete in my program. So i am using the logic to get system time when i call the function and time when the function returns a value then by subtracting the values i get time it took to complete. So if anyone can tell me some better approach or just how to get system time at an instance it would be quite a help

like image 514
Mobin Avatar asked Mar 01 '09 05:03

Mobin


People also ask

How do I get the current time in C?

time() function in C. The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds.

What is time_t type in C?

The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard <time. h> header.

What type of value is returned by the time () function?

Returns the decimal number for a particular time. If the cell format was General before the function was entered, the result is formatted as a date. The decimal number returned by TIME is a value ranging from 0 (zero) to 0.99988426, representing the times from 0:00:00 (12:00:00 AM) to 23:59:59 (11:59:59 P.M.).


2 Answers

The approach I use when timing my code is the time() function. It returns a single numeric value to you representing the epoch which makes the subtraction part easier for calculation.

Relevant code:

#include <time.h>
#include <iostream>

int main (int argc, char *argv[]) {

int startTime, endTime, totalTime;

startTime = time(NULL);

/* relevant code to benchmark in here */

endTime = time(NULL);

totalTime = endTime - startTime;

std::cout << "Runtime: " << totalTime << " seconds.";

return 0;
}

Keep in mind this is user time. For CPU, time see Ben's reply.

like image 75
John T Avatar answered Sep 28 '22 19:09

John T


Your question is totally dependant on WHICH system you are using. Each system has its own functions for getting the current time. For finding out how long the system has been running, you'd want to access one of the "high resolution performance counters". If you don't use a performance counter, you are usually limited to microsecond accuracy (or worse) which is almost useless in profiling the speed of a function.

In Windows, you can access the counter via the 'QueryPerformanceCounter()' function. This returns an arbitrary number that is different on each processor. To find out how many ticks in the counter == 1 second, call 'QueryPerformanceFrequency()'.

If you're coding under a platform other than windows, just google performance counter and the system you are coding under, and it should tell you how you can access the counter.

Edit (clarification)
This is c++, just include windows.h and import the "Kernel32.lib" (seems to have removed my hyperlink, check out the documentation at: http://msdn.microsoft.com/en-us/library/ms644904.aspx). For C#, you can use the "System.Diagnostics.PerformanceCounter" class.

like image 28
Grant Peters Avatar answered Sep 28 '22 17:09

Grant Peters