Skip to main content

Microsoft Silverlight

Answered Question Lack of resolution in DateTime.TicksRSS Feed

(0)

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Lack of resolution in DateTime.Ticks

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?

DDtMM

mrjvdveen
mrjvdveen

Participant

Participant

1937 points

366 Posts

Re: Lack of resolution in DateTime.Ticks

 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.

-------------
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Re: Lack of resolution in DateTime.Ticks

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.

DDtMM

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15894 points

1,541 Posts

Re: Lack of resolution in DateTime.Ticks

Hi DDtMM,

Did you fix the problem?

actually, according to the msdn document

  • 1 second = 1000 millisecond
  • 1 millisecond = 10000 ticks 

Thanks,

 

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

mrjvdveen
mrjvdveen

Participant

Participant

1937 points

366 Posts

Re: Lack of resolution in DateTime.Ticks

 Mmmm, must have misread the document. Embarrassed Sorry about that.

-------------
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Re: Lack of resolution in DateTime.Ticks

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.

DDtMM

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15894 points

1,541 Posts

Re: Lack of resolution in DateTime.Ticks

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

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Re: Lack of resolution in DateTime.Ticks

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. 

datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:0environment.tickscount0
datetime.now.ticks:0environment.tickscount0
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:0environment.tickscount0
datetime.now.ticks:312500environment.tickscount31
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:0environment.tickscount0
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount16
datetime.now.ticks:156250environment.tickscount15
datetime.now.ticks:156250environment.tickscount16

 

DDtMM

sl.ayer
sl.ayer

Participant

Participant

852 points

164 Posts

Answered Question

Re: Lack of resolution in DateTime.Ticks

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.

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Re: Lack of resolution in DateTime.Ticks

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?

DDtMM

sl.ayer
sl.ayer

Participant

Participant

852 points

164 Posts

Re: Lack of resolution in DateTime.Ticks

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.

DDtMM
DDtMM

Member

Member

86 points

101 Posts

Answered Question

Re: Lack of resolution in DateTime.Ticks

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.

DDtMM
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities