cpu time

Upload: marsala-nisto

Post on 14-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 CPU time

    1/9

    ENAE380 Flight Softw are Sys tems

    UNIX Time & CPU time

  • 7/27/2019 CPU time

    2/9

    ENAE380 Flight Softw are Sys tems

    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 Gregoriancalendar Coordinated Universal Time (UTC) of January1, 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

  • 7/27/2019 CPU time

    3/9

    ENAE380 Flight Softw are Sys tems

    /* Reading time. */

    void ReadTime()

    {time_t now; // integer valuestruct 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);

    };

    struct tm

    member(int)

    description

    tm_hour hour (0 - 23)

    tm_isdst Daylight savings

    tm_mday Day of month (1-31)

    tm_min Minutes (0-59)

    tm_mon Months (0-11, 0=January)

    tm_sec Seconds (0-60, 60=leap

    sec)

    tm_wday Day of week (0-6,

    0=Sunday)tm_yday Day of the year (0-365)

    tm_year Year since 1900 (110=2010)

  • 7/27/2019 CPU time

    4/9

    ENAE380 Flight Softw are Sys tems

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

  • 7/27/2019 CPU time

    5/9

    ENAE380 Flight Softw are Sys tems

    /* File: hr_time.h */#pragma once#include #include

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

    };

  • 7/27/2019 CPU time

    6/9

    ENAE380 Flight Softw are Sys tems

    /* File: hr_time.cppDetermine the amount of CPU time usedbetween the start and stop of astopwatch.

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

    }

  • 7/27/2019 CPU time

    7/9

    ENAE380 Flight Softw are Sys tems

    /// Return the current cpu timedouble CStopWatch::getTime( ){

    QueryPerformanceCounter(&t);

    return LIToSecs(t);}

    /// Return elapsed time in secondsdouble 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);

    }

    /////////////////

  • 7/27/2019 CPU time

    8/9

    ENAE380 Flight Softw are Sys tems

    /* vbanesBanes, VinceProject: CPUtimeFile: CPUtime.cpp */

    #include "hr_time.h"void _tmain(){

    CStopWatch sw;

    FILE *f;fopen_s(&f, "vbanes_time.txt", "wt");

    sw.startTimer(); // Start the stop watchsw.stopTimer(); // Stop the stop watch

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

    }

  • 7/27/2019 CPU time

    9/9

    ENAE380 Flight Softw are Sys tems

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