Vous êtes sur la page 1sur 9

UNIX Time & CPU time

ENAE380 Flight Software Systems

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.

Integer seconds, no fractional part


Originally, it was a 32-bit integer (~68 years) Overflows in the year 2038 UTC time was invented in 1972 Does not include leap seconds Updated in modern systems to 64 bits

ENAE380 Flight Software Systems

/* 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); };

ENAE380 Flight Software Systems

CPU Task Time


Task CPU time

The epoch begins when the current task is started.


The time is turned off while the task is inactive Reading the CPU clock gives a good indication of the amount of elapsed time require to complete a task.

hr_time.cpp
Reads the internal CPU clock and convert to seconds Windows system routines
QueryPerformanceFrequency(LARGE_INTEGER *frequency);

QueryPerformanceCounter(LARGE_INTEGER *t);

ENAE380 Flight Software Systems

/* File: hr_time.h #pragma once #include <windows.h> #include <tchar.h>

*/

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); };

ENAE380 Flight Software Systems

/* 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); }

ENAE380 Flight Software Systems

/// 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); } /////////////////

ENAE380 Flight Software Systems

/* 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

fprintf_s(f, time = %10.3f usec\n, sw.getElapsedTime()*1000000.0); fclose(f); }

ENAE380 Flight Software Systems

#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);

ENAE380 Flight Software Systems

Vous aimerez peut-être aussi