Need help understanding system timestamp

Discussion in 'Programmer's Corner' started by integral, Dec 7, 2010.

  1. integral

    Thread Starter New Member

    Aug 19, 2010
    22
    0
    Hello,
    I need help understanding the time output I am getting from a for loop. In the code I am just running a for loop and having the system output the current time to a file.
    computer Information (from control panel)
    Computer:
    processor 1000MHz
    999 MHz, 960 MB of RAM
    I am using visual studio express 2008 on windows XP.
    The code is below


    Here is a link to the output text file: http://sharesend.com/p60vp


    here is the format of the ouput text file
    Code ( (Unknown Language)):
    1.  
    2.                          HH:MM:SS*milliSeconds
    3.  t *0000* localTime1 is: 12:17:27*812
    4.  
    After I exported the text file to excel, I noticed the following:


    Code ( (Unknown Language)):
    1.  
    2.                           diff between current time and next time (ms)
    3.  276 output lines of 12:13:19.296 00:00:00.016
    4.  247 output lines of 12:13:19.312 00:00:00.016
    5.  278 output lines of 12:13:19.328 00:00:00.015
    6.  276 output lines of 12:13:19.343 00:00:00.016
    7.  282 output lines of 12:13:19.359 00:00:00.016
    8.  273 output lines of 12:13:19.375 00:00:00.015
    9.  287 output lines of 12:13:19.390 00:00:00.016
    10.  268 output lines of 12:13:19.406 00:00:00.015
    11.  288 output lines of 12:13:19.421 00:00:00.016
    12.  290 output lines of 12:13:19.437 00:00:00.016
    13.  280 output lines of 12:13:19.453    
    14.  

    Code ( (Unknown Language)):
    1.  
    2.  [COLOR=#0000ff][SIZE=2]#include[/SIZE][/COLOR][COLOR=#a31515][SIZE=2]<iostream>[/SIZE][/COLOR]
    3.  [COLOR=#0000ff][SIZE=2]#include[/SIZE][/COLOR][COLOR=#a31515][SIZE=2]<fstream>[/SIZE][/COLOR]
    4.  [COLOR=#0000ff][SIZE=2]#include[/SIZE][/COLOR][COLOR=#a31515][SIZE=2]<iomanip>[/SIZE][/COLOR]
    5.  [COLOR=#0000ff][SIZE=2]#include[/SIZE][/COLOR][COLOR=#a31515][SIZE=2]<windows.h>[/SIZE][/COLOR]
    6.  [COLOR=#0000ff][SIZE=2]using[/SIZE][/COLOR][COLOR=#0000ff][SIZE=2]namespace[/SIZE][/COLOR][COLOR=#000000][SIZE=2] std;[/SIZE][/COLOR]
    7.  
    8.  
    9.  [COLOR=#0000ff][SIZE=2]int[/SIZE][/COLOR][COLOR=#000000][SIZE=2] main()[/SIZE][/COLOR]
    10.  [COLOR=#000000][SIZE=2]{[/SIZE][/COLOR]
    11.  [COLOR=#000000][SIZE=2]    SYSTEMTIME localTime0, localTime1, localTime2;[/SIZE][/COLOR]
    12.  [COLOR=#000000][SIZE=2]    ofstream myFile;[/SIZE][/COLOR]
    13.  [COLOR=#000000][SIZE=2]    myFile.open([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"outputFile.txt"[/SIZE][/COLOR][COLOR=#000000][SIZE=2]);[/SIZE][/COLOR]
    14.  
    15.  
    16.  [COLOR=#000000][SIZE=2]    GetLocalTime(&localTime0);[/SIZE][/COLOR]
    17.  [COLOR=#000000][SIZE=2]    myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"Before Counter - localTime0 is: "[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime0.wHour <<[/SIZE][/COLOR]
    18.  [COLOR=#000000][SIZE=2]        [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime0.wMinute << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2])  [/SIZE][/COLOR]
    19.  [COLOR=#000000][SIZE=2]        << localTime0.wSecond << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"."[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(3) << localTime0.wMilliseconds << endl;[/SIZE][/COLOR]
    20.  [COLOR=#000000][SIZE=2]    myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"=============================================\n"[/SIZE][/COLOR][COLOR=#000000][SIZE=2];[/SIZE][/COLOR]
    21.  
    22.  
    23.  [COLOR=#000000][SIZE=2]    myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"                        HH:MM:SS*milliSeconds"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << endl;[/SIZE][/COLOR]
    24.  
    25.  
    26.  [COLOR=#000000][SIZE=2]    [/SIZE][/COLOR][COLOR=#0000ff][SIZE=2]for[/SIZE][/COLOR][COLOR=#000000][SIZE=2] ([/SIZE][/COLOR][COLOR=#0000ff][SIZE=2]int[/SIZE][/COLOR][COLOR=#000000][SIZE=2] t = 0; t < 5000; t++)[/SIZE][/COLOR]
    27.  [COLOR=#000000][SIZE=2]    {[/SIZE][/COLOR]
    28.  [COLOR=#000000][SIZE=2]        GetLocalTime(&localTime1);[/SIZE][/COLOR]
    29.  [COLOR=#000000][SIZE=2]        myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"t *"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(4) << t << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"* "[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"localTime1 is: "[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) <<[/SIZE][/COLOR]
    30.  [COLOR=#000000][SIZE=2]            localTime1.wHour << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime1.wMinute << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] <<[/SIZE][/COLOR]
    31.  [COLOR=#000000][SIZE=2]            setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2])  << localTime1.wSecond << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"*"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(3) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2])  << localTime1.wMilliseconds << endl;[/SIZE][/COLOR]
    32.  [COLOR=#000000][SIZE=2]        [/SIZE][/COLOR]
    33.  [COLOR=#000000][SIZE=2]    }[/SIZE][/COLOR]
    34.  [COLOR=#000000][SIZE=2]    myFile << endl;[/SIZE][/COLOR]
    35.  
    36.  
    37.  [COLOR=#000000][SIZE=2]    myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"=============================================\n"[/SIZE][/COLOR][COLOR=#000000][SIZE=2];[/SIZE][/COLOR]
    38.  [COLOR=#000000][SIZE=2]    GetLocalTime(&localTime2);[/SIZE][/COLOR]
    39.  [COLOR=#000000][SIZE=2]    myFile << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"After Counter - localTime2 is: "[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime2.wHour <<[/SIZE][/COLOR]
    40.  [COLOR=#000000][SIZE=2]        [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime2.wMinute <<    [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]":"[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(2) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2])[/SIZE][/COLOR]
    41.  [COLOR=#000000][SIZE=2]        << localTime2.wSecond << [/SIZE][/COLOR][COLOR=#a31515][SIZE=2]"."[/SIZE][/COLOR][COLOR=#000000][SIZE=2] << setw(3) << setfill([/SIZE][/COLOR][COLOR=#a31515][SIZE=2]'0'[/SIZE][/COLOR][COLOR=#000000][SIZE=2]) << localTime2.wMilliseconds << endl;[/SIZE][/COLOR]
    42.  
    43.  
    44.  [COLOR=#000000][SIZE=2]    myFile.close();[/SIZE][/COLOR]
    45.  
    46.  
    47.  [COLOR=#000000][SIZE=2]    [/SIZE][/COLOR][COLOR=#0000ff][SIZE=2]return[/SIZE][/COLOR][COLOR=#000000][SIZE=2] 0;[/SIZE][/COLOR]
    48.  [COLOR=#000000][SIZE=2]}[/SIZE][/COLOR]
    49.  
    My questions are:
    1. Why is there a difference in the number of occurrences for each time stamp? For example, why does 12:13:19.296 occur 276 times while 12:13:19.312 occurs 247 times?


    2. Why is there a difference when the difference of successive timestamps is calculated. For example, 12:13:19.312 – 12:13:19.296 = 16 ms but 12:13:19.343 - 12:13:19.328 = 15 ms
     
  2. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,346
    Hello,

    I think the time used is on tye boarder of 15 and 16 mSec.
    The resolution is the main factor of the switching between 15 and 16 mSec.

    Bertus
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    That isn't the only program running on the computer. Unless you set it to high priority, and have 4 or more cores, it won't show results as if it is the only thing running, the OS itself uses a good deal of processor, not to mention all the little programs sitting in the systray by the clock.
     
  4. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    Windows, Macs or Linux systems are fine for most things that people want to do, like send email or browse the web, but with those OSes, your program really doesn't have complete control of its execution. CPU time is shared among several programs.

    When it is important to a project to have control over the exact execution times, people will use real-time operating systems, which do give precise control over how a program runs, or microcontrollers, which really don't have an operating system - they only run one piece of code, which automatically gives that code complete control over how it runs.
     
Loading...