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



Will you give this article a +1 ? Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

4 Responses to "Calculate the Seconds, Milliseconds and Microseconds using C# and VB.NET"
  1. Kevin said...
    August 29, 2009 at 10:30 AM

    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.

  2. Erik said...
    August 29, 2009 at 6:58 PM

    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.

  3. Markus Olsson said...
    August 30, 2009 at 12:06 PM

    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.

  4. Suprotim Agarwal said...
    August 31, 2009 at 12:52 AM

    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!

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions