Académique Documents
Professionnel Documents
Culture Documents
Relative Time
All time is relative. Epoch: defines the zero point for a selected era UNIX (POSIX) time (ISO 8601) Time is seconds since midnight proleptic Gregorian calendar Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.
/* Reading time.
*/
struct tm
member (int)
tm_hour
tm_isdst tm_mday tm_min tm_mon tm_sec tm_wday tm_yday tm_year
description
hour (0 - 23)
Daylight savings Day of month (1-31) Minutes (0-59) Months (0-11, 0=January) Seconds (0-60, 60=leap sec) Day of week (0-6, 0=Sunday) Day of the year (0-365) Year since 1900 (110=2010)
void ReadTime() { time_t now; // integer value struct tm *ts; char buf[80]; /* Get the current time */ now = time(NULL); /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */ ts = localtime(&now); strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts); printf(%s\n, buf); printf(year=%d\n, ts->tm_year); };
hr_time.cpp
Reads the internal CPU clock and convert to seconds Windows system routines
QueryPerformanceFrequency(LARGE_INTEGER *frequency);
QueryPerformanceCounter(LARGE_INTEGER *t);
*/
class CStopWatch { public: CStopWatch(); void startTimer( ); void stopTimer( ); double getElapsedTime(); double getTime(); private: typedef struct { LARGE_INTEGER start; LARGE_INTEGER stop; } stopWatch; stopWatch timer; LARGE_INTEGER frequency; LARGE_INTEGER t; double LIToSecs(LARGE_INTEGER &L); };
/* File: hr_time.cpp Determine the amount of CPU time used between the start and stop of a stopwatch. */ #include "hr_time.h" CStopWatch::CStopWatch() { timer.start.QuadPart = 0; timer.stop.QuadPart = 0; QueryPerformanceFrequency(&frequency); } void CStopWatch::startTimer( ) { QueryPerformanceCounter(&timer.start); } void CStopWatch::stopTimer( ) { QueryPerformanceCounter(&timer.stop); }
/// Return the current cpu time double CStopWatch::getTime( ) { QueryPerformanceCounter(&t); return LIToSecs(t); } /// Return elapsed time in seconds double CStopWatch::getElapsedTime() { LARGE_INTEGER time; time.QuadPart = timer.stop.QuadPart timer.start.QuadPart; return LIToSecs(time); } double CStopWatch::LIToSecs(LARGE_INTEGER &L) { return ((double)L.QuadPart / (double)frequency.QuadPart); } /////////////////
/* vbanes Banes, Vince Project: CPUtime File: CPUtime.cpp #include "hr_time.h" void _tmain() { CStopWatch sw;
*/
FILE *f; fopen_s(&f, "vbanes_time.txt", "wt"); sw.startTimer(); sw.stopTimer(); // Start the stop watch // Stop the stop watch
#include "hr_time.h" void _tmain() { CStopWatch sw; FILE *f; fopen_s(&f, "vbanes_time.txt", "wt"); sw.startTimer(); double x = 0.0; x = sin(x); sw.stopTimer(); fprintf_s(f, time for sin()= %10.3f usec\n", sw.getElapsedTime()*1000000.0); fclose(f);