Convert ticks to milliseconds

N

Nicholas Paldino [.NET/C# MVP]

Even better would be to use the constructor of the TimeSpan class which
encapsulates the logic, and takes it out of your hands:

TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks);
double ms = ts.TotalMilliseconds;
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Nicholas said:
Even better would be to use the constructor of the TimeSpan class which
encapsulates the logic, and takes it out of your hands:

TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks);
double ms = ts.TotalMilliseconds;

It encapsulates the logic. But the only case where
it encapsulate better than /TimeSpan.TicksPerMillisecond
is if the correlation between Ticks and Milliseconds are
changed to something non proportional. I very much doubt
that would happen.

Arne
 
E

Ebbe Kristensen

Arne said:
It encapsulates the logic. But the only case where
it encapsulate better than /TimeSpan.TicksPerMillisecond
is if the correlation between Ticks and Milliseconds are
changed to something non proportional. I very much doubt
that would happen.

You are right only if ticks are not dependent on processor clock frequency.

If they are, do remember that lowering the clock frequency to save power is
becoming widely used in portable devices.

Ebbe
 
M

Marc Gravell

In this context, a tick is defined as 100 nanoseconds. It is not
machine dependent.

I would expect lots of pain if this ever changed...

Marc
 
E

Ebbe Kristensen

Marc said:
In this context, a tick is defined as 100 nanoseconds. It is not
machine dependent.

So the value of 'TimeSpan.TicksPerMillisecond' is always 10 no matter what
system, you are working with? Is it guaranteed that it will be 10 in future
systems?
I would expect lots of pain if this ever changed...

Yes, it could create non-trivial problems :)

Ebbe
 
M

Marc Gravell

I don't know how to answer that directly, but *at the minimum* I
strongly doubt that it would ever change to be non-proportional wrt
time; anybody with a more formal answer?

Marc
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ebbe said:
So the value of 'TimeSpan.TicksPerMillisecond' is always 10 no matter what
system, you are working with? Is it guaranteed that it will be 10 in future
systems?

It does not need to be constant over time for the division to be
valid. The division only requires the relationship between ticks
and time to be proportional.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top