Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

How to measure time spans in .NET?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 29 Apr 2005 21:17 GMT
I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in
my VC++ 6.0 projects to measure correct time spans, for example for timeouts,
how much time takes to execute certain method etc

I wonder whether there is something similar in .NET to measure time spans?

Meantime I still use these methods through DllImport
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

I suspect there is better approach then using DllImport. Can anyone suggest
something better?

Thanks
Kapil Khosla [MSFT] - 02 May 2005 21:37 GMT
> I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in
> my VC++ 6.0 projects to measure correct time spans, for example for timeouts,
[quoted text clipped - 10 lines]
>
> Thanks

Actually, that if you want high precision timing, I still recommend using
explicit Pinvoke the way you describe. In .NET you have access to
System.DateTime structure which is stated as 100 nanoseconds (1 / 10
millionth of a second). If that precision is good enough you should be able
to use that instead.

Thanks,
Kapil
Carl Daniel [VC++ MVP] - 03 May 2005 02:34 GMT
> Actually, that if you want high precision timing, I still recommend
> using explicit Pinvoke the way you describe. In .NET you have access
> to System.DateTime structure which is stated as 100 nanoseconds (1 /
> 10 millionth of a second). If that precision is good enough you
> should be able to use that instead.

Just to be clear:  DateTime has 100 nanosecond resolution.  It does not, by
any stretch have 100 nanosecond accuracy or precision.  Rather, the
precision and accuracy are on the order of +/- 5-7ms (10-15ms tick time).

-cd
William DePalo [MVP VC++] - 03 May 2005 02:48 GMT
> Just to be clear:  DateTime has 100 nanosecond resolution.  It does not,
> by any stretch have 100 nanosecond accuracy or precision.  Rather, the
> precision and accuracy are on the order of +/- 5-7ms (10-15ms tick time).

Absolutely right.

FWIW: while there are no guarantees as to minimum latencies on any version
of Windows (except _maybe_ CE, I dunno) you can get the precison down as low
as it will go by bracketing time sensitive sections of code with

   timeBeginPeriod(1);

and

   timeEndPeriod(1);

It's not at all obvious, but things like the smallest period you can Sleep()
change in the presence of those two inoccuous looking lines.

Regards,
Will

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.