Solved

Time stamping events in c# using TimeOfDay results in intervalised results

Posted on 2010-08-20
6
507 Views
Last Modified: 2013-12-17

I wrote a simple console app in c# that outputs record updates of streaming data that can be handled through different real-time APIs . I wanted to timestamp to atleast milisec granularity as per below

 static void OnRecord.Update(Record rec, bool stale, int index, ref System.Array indexArr, ref System.Array valueArr)
        {
           


           for (int i = 0; i < valueArr.Length; i++)
            {
                try
                {
                    Console.WriteLine(rec[index].Name + ":"+ valueArr.GetValue(i) + ":" + DateTime.Now.TimeOfDay);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: {0}", ex.Message);
                }
            }

the output looked at first glance fine giving name:new value:timestamp as per below

GBL:+20592322:11:41:42.2629255
GBL:+20593312:11:41:42.2629255
GBL:+20594302:11:41:42.4816839
GBL:+20595292:11:41:42.5910631
GBL:+20595677:11:41:43.2629639
GBL:+20596685:11:41:43.2785895
GBL:+20597675:11:41:43.8567367
GBL:+20598402:11:41:46.5599655
GBL:+20599061:11:41:46.5912167
GBL:+20600305:11:41:51.4976551
GBL:+20601096:11:41:51.5132807
GBL:+20601992:11:42:07.8576583
GBL:+20603992:11:42:13.6391303
GBL:+20605992:11:42:13.6391303
GBL:+20608992:11:42:13.6391303
GBL:+20609420:11:42:13.7485095
GBL:+20609520:11:42:13.7485095
GBL:+20609609:11:42:45.8434919
GBL:+20610009:11:42:45.8591175

however on inspection the timestamp are always intervalise by a factor of 0.0156256
Even if I run seperate instances of the app with diffrent record feeds,using diffrent apis for the record event handling then compare the results, the difference in timestamps is stll a factor of this value.
 It's as though running  TimeOfday irrespective if which instance of the app and event  it's calling  only returns results after so many clock cycles.  As the data is coming over the network I also can verify the events through wireshark so know its not the data itself causing this intervalising behaviour.I could understand it if the value was much lower but wouldn't expect it to be 15.6 msec on a modern processor. Perhaps my understanding of the windows Timeofday call  is completely wrong ?
0
Comment
Question by:windymillermc
  • 3
  • 3
6 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33487390
That's standard behavior...

Don't forget that WINDOWS itself gives the illusion of "multi-tasking" by allocating only a small time-slice to each process and its threads.  The "intervalised" gap in-between is probably due to the operation system allowing other processes to do their thing.

If your app was running on specialized hardware where one app was able to monopolize the operating system then perhaps you might see what you expect where the values change smoothly.  (this is theoretical!)

FYI, Windows hasn't been non-preemptive  since version 3.1 back in 1992!
http://en.wikipedia.org/wiki/Preemption_(computing)
http://technet.microsoft.com/en-us/library/cc767883.aspx
0
 

Author Comment

by:windymillermc
ID: 33487457
But if that was the case I would see similar behaviour with wireshark which runs and timestamps packets on the same machine
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33487514
What language was WireShark written in?  It might lower level optimized code...

.Net is one of the most bloated pieces of framework on the planet!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Closing Comment

by:windymillermc
ID: 33541016
The answer was correct in why the behaviour was happeing abut it was't a solution as to how to fix the code.
Found the real solution by using DateTimePrecise class written by James Brock
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33541433
0
 

Author Comment

by:windymillermc
ID: 33545659
ummm well I have now build the project class and used it but it may slightly more granularity but the 15millisec interval is still there. Have you tried it ?
Actually, re-reading the dialogue carefully it does say it has an accuracy of 15 miilisecond but I thought that was a typo as otherwise what's the point of it. So it doesn't do what I expected.
So I'm still searching for a solution
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now