Calculate the Seconds, Milliseconds and Microseconds using C# and VB.NET

Recently I was helping out a colleague to calculate the difference between two DateTime. The requirement was to calculate the difference to the Milliseconds and Microsecond level. Here’s what I suggested using DateTime.Ticks. A single tick represents one hundred nanoseconds or one ten-millionth of a second.

Update: As mentioned by Markus Olsson and others in the comments section, an accurate and a better way of measuring time is to use the StopWatch class. Check this post.

C#

using System;

class Program
{
static void Main(string[] args)
{
try
{
long startTick = DateTime.Now.Ticks;
long endTick = DateTime.Now.Ticks;
long tick = endTick - startTick;
long seconds = tick / TimeSpan.TicksPerSecond;
long milliseconds = tick / TimeSpan.TicksPerMillisecond;
long microseconds = tick / 10;
Console.WriteLine("Seconds Elapsed :" + seconds);
Console.WriteLine("Millseconds Elapsed :" + milliseconds);
Console.WriteLine("Microseconds Elapsed :" + microseconds);
Console.ReadLine();
}
catch (Exception ex)
{
// handle ex
}
}
}

VB.NET

Imports System

Friend Class Program
Shared Sub Main(ByVal args() As String)
Try
Dim
startTick As Long = DateTime.Now.Ticks
Dim endTick As Long = DateTime.Now.Ticks
Dim tick As Long = endTick - startTick
Dim seconds As Long = tick / TimeSpan.TicksPerSecond
Dim milliseconds As Long = tick / TimeSpan.TicksPerMillisecond
Dim microseconds As Long = tick \ 10
Console.WriteLine("Seconds Elapsed :" & seconds)
Console.WriteLine("Millseconds Elapsed :" & milliseconds)
Console.WriteLine("Microseconds Elapsed :" & microseconds)
Console.ReadLine()
Catch ex As Exception
' handle ex
End Try
End Sub
End Class




OUTPUT

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

4 comments:

Kevin said...

The DateTime type implements an operator for subtraction that returns a TimeSpan object. This would be my suggestion:

//-----------
DateTime start = DateTime.Now;
Thread.Sleep(10);
DateTime end = DateTime.Now;
TimeSpan span = end - start;
Console.WriteLine("Ticks: " + span.Ticks);
//-----------

From there, you can access the properties of span to access the number of days, hours, minutes, seconds... down to number of ticks.

To me this seems more accurate too? I could be wrong though.

Erik said...

The problem with this is that TimeSpan only has a resolution of 100ms on an NT4 or better system - which means you can't rely on it being accurate for anything less than that amount, give or take. If you really need accurate timing at those levels, you need the high performance timer APIs or multimedia timer APIs.

Markus Olsson said...

First; I totally agree with Kevin that this is a messy way of calculating the elapsed time between two DateTimes.

And second. Eric hinted that using DateTime.Now for timing sure is bad. Although the limitation does not lie within the TimeSpan struct, but with the underlying implementation of DateTime.Now. For timing you should use the Stopwatch class in System.Diagnostics. See this stackoverflow answer for more detail.

Suprotim Agarwal said...

Thanks Kevin, Erik and Markus. That was quite informative. The updates have been made to the original post for the benefit of future users and webdevs like me!