views:

459

answers:

4

The title is pretty much self-explanatory, I'm killing myself over this simplicity.

Looked here, but it isn't much helpful.

+10  A: 

I think that the Stopwatch class is what you are looking for.

Guffa
+1, For timing (vs. getting the absolute time), Stopwatch is it.
orip
Stopwatch is definitely the best way to time things in the BCL, it's just not accurate to nanoseconds.
overstood
@overstood: Yes, the accuracy is not down to nanoseconds, just like the nanoTime method. :)
Guffa
+2  A: 

DateTime.Now will give you the current time in milliseconds, but time that is accurate to nanoseconds is fairly impractical, at least in Windows.

nasufara
I believe the accuracy of DateTime.Now is ~15 or 20 milliseconds. It has more precision than accuracy.
Snarfblam
A: 

the closest thing that i could find is the DateTime.ToFileTime() method. you can call this on an instance of a DateTime like so:

long starttime = DateTime.Now.ToFileTime()

The method returns a Windows File Time:

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).

you could at least time down to 100 ns intervals with it.

src: http://msdn.microsoft.com/en-us/library/system.datetime.tofiletime.aspx

Scott M.
This only gives you 100 ns accuracy for the time of the last windows timer update, which happens every 15 to 16 ms
overstood
ok, just out of curiosity wheres that info coming from?
Scott M.
A: 

I think you're going to hit the hard limits of the OS if you're timing in nanoseconds. Here's a good article on the topic:

http://www.lochan.org/2005/keith-cl/useful/win32time.html

While Windows will happily return 100 nanosecond accuracy, the clock is only guaranteed to update once every 15.6 milliseconds or so. So effectively Windows returns the time at which those updates occurred to 100 nanosecond accuracy. For more accuracy than this you probably need to be prepared to write C or assembler and run and embedded OS.

overstood