Skip to main content
Home Forums Silverlight Programming Game Development Lack of resolution in DateTime.Ticks
11 replies. Latest Post by DDtMM on August 26, 2009.
(0)
DDtMM
Member
86 points
101 Posts
08-20-2009 8:46 PM |
Is anybody else getting bummed by the poor resolution in DateTime.Ticks. I mean I'm getting a difference of 0 ticks accross multiple frames which is causing me to lock down my frame rates for one.
Has anybody found a way around this?
mrjvdveen
Participant
1937 points
366 Posts
08-21-2009 2:45 AM |
The API documentation for the DateTime class specifies that a Tick is 0.0001 seconds. On a 2GHz CPU (single core) a CPU instruction occurs every 0.0000000005 seconds. That means a single Tick can contain 200000 CPU instructions. That seems like it's not suitable timing short operations.
You could try with Miliseconds (and seconds). This would give you more acuracy. A milisecond in the above example only contains 2000 CPU instructions and may give you measurable results.
It would be helpful if you elaborate on what you're trying to achieve.
HTH.
08-21-2009 9:36 AM |
So wait! A millisecond has about 100 times better resolution than a Tick? Well I just assumed that Tick would be the lowest resolution. That would solve my problem, thanks.
Mog Lian...
All-Star
15894 points
1,541 Posts
08-25-2009 4:52 AM |
Hi DDtMM,
Did you fix the problem?
actually, according to the msdn document
Thanks,
08-25-2009 6:21 AM |
Mmmm, must have misread the document. Sorry about that.
08-25-2009 10:13 AM |
I realize now I didn't phrase the question correctly. THe problem is the resolution of DateTime.Now. I don't have that exact numbers on hand but the interval is something like 15 milliseconds. That's the bummer. I need something more accurate than what I'm getting: I need a high precision timer.
I tried changing from ticks to milliseconds but it didn't help the resolution problem.
I tried Environment.TickCount but it has the same problem. I either get 0 or 16 in difference values.
It could be that the problem is the timers that I am using, but it doesn't seem to make sense that it bounces around 0 and 16.
08-25-2009 10:31 PM |
Hi,
How do you get the interval? By default, there are 60 frames per second in silverlight, which means it's 15-16 millseconds between frames.
If we calculate interval in CompositionTarget.Rendering event, then we would get 15-16 millseconds timespan.
To be more accurate, we could use TimeSpan.Ticks property, check this sample
public MainPage() { InitializeComponent(); CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); } int precount; DateTime pretime; void CompositionTarget_Rendering(object sender, EventArgs e) { string str = "datetime.now.ticks:"; DateTime time = DateTime.Now; str += (time-pretime).Ticks; pretime = time; str += "environment.tickscount"; int count = Environment.TickCount; str += count-precount; precount = count; textblock1.Text = str; }
08-26-2009 7:48 AM |
Here is a representative sample from the middle of the results. I think it illustrates the problem I am experiencing. I have max framerate at 300 by the way.
sl.ayer
852 points
164 Posts
08-26-2009 5:45 PM |
The behavior you seeing is to be expected since I am positive that DateTime.Now uses standard windows timer functions. Those functions still rely on timer interrupts (same as 20 years ago). Default interrupt rate for XP is 64 interrupts per second I believe. It used to be 20 on win95, so things are much better now. If you were programming native application you would have access to high resolution timers (QueryPerformanceCounter) or you'd be able to set your own resolution for system timer up to 1ms. But since we don't have access to native functions we must avoid relying on high precision timing in our applications. The way I am handling timing is by measuring time across multiple frames. Instead of measuring how long it takes to render one frame, you could measure how many frames you render every second or half second and then inverse it to get time per frame.
08-26-2009 5:52 PM |
I had the exact same idea this morning and rewrote my timer to measure the average time over 10 frames. It works much better than what I used to have. Still it would be nice to see this in SL4?
08-26-2009 6:03 PM |
See what in SL4? High resolution timers? Not likely, since they are native to windows and I don't know if Mac has direct counterpart.
08-26-2009 6:18 PM |
After a brief web search I have confirmed that it is part of the kernel for OS X. I wouldn't be surprised if there wasn't a Linux equivelant for Moonlight as well.