Need help understanding system timestamp

Thread Starter

integral

Joined Aug 19, 2010
22
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
Rich (BB code):
                         HH:MM:SS*milliSeconds
 t *0000* localTime1 is: 12:17:27*812
After I exported the text file to excel, I noticed the following:


Rich (BB code):
                          diff between current time and next time (ms)
 276 output lines of 12:13:19.296 00:00:00.016
 247 output lines of 12:13:19.312 00:00:00.016
 278 output lines of 12:13:19.328 00:00:00.015
 276 output lines of 12:13:19.343 00:00:00.016
 282 output lines of 12:13:19.359 00:00:00.016
 273 output lines of 12:13:19.375 00:00:00.015
 287 output lines of 12:13:19.390 00:00:00.016
 268 output lines of 12:13:19.406 00:00:00.015
 288 output lines of 12:13:19.421 00:00:00.016
 290 output lines of 12:13:19.437 00:00:00.016
 280 output lines of 12:13:19.453

Rich (BB code):
 #include<iostream>
 #include<fstream>
 #include<iomanip>
 #include<windows.h>
 usingnamespace std;
 

 int main()
 {
     SYSTEMTIME localTime0, localTime1, localTime2;
     ofstream myFile;
     myFile.open("outputFile.txt");
 

     GetLocalTime(&localTime0);
     myFile << "Before Counter - localTime0 is: " << setw(2) << setfill('0') << localTime0.wHour <<
         ":" << setw(2) << setfill('0') << localTime0.wMinute << ":" << setw(2) << setfill('0')   
         << localTime0.wSecond << "." << setw(3) << localTime0.wMilliseconds << endl;
     myFile << "=============================================\n";
 

     myFile << "                        HH:MM:SS*milliSeconds" << endl;
 

     for (int t = 0; t < 5000; t++)
     {
         GetLocalTime(&localTime1);
         myFile << "t *" << setw(4) << t << "* " << "localTime1 is: " << setw(2) << setfill('0') <<
             localTime1.wHour << ":" << setw(2) << setfill('0') << localTime1.wMinute << ":" <<
             setw(2) << setfill('0')  << localTime1.wSecond << "*" << setw(3) << setfill('0')  << localTime1.wMilliseconds << endl;
         
     }
     myFile << endl;
 

     myFile << "=============================================\n";
     GetLocalTime(&localTime2);
     myFile << "After Counter - localTime2 is: " << setw(2) << setfill('0') << localTime2.wHour <<
         ":" << setw(2) << setfill('0') << localTime2.wMinute <<    ":" << setw(2) << setfill('0')
         << localTime2.wSecond << "." << setw(3) << setfill('0') << localTime2.wMilliseconds << endl;
 

     myFile.close();
 

     return 0;
 }
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
 

bertus

Joined Apr 5, 2008
22,994
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
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

davebee

Joined Oct 22, 2008
540
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.
 
Top