473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to measure time spans in .NET?

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
Nov 17 '05 #1
3 3844


"Steve" wrote:
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


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

Nov 17 '05 #2
Kapil Khosla [MSFT] wrote:
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
Nov 17 '05 #3
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
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


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
7
by: Peng Yu | last post by:
Hi, For example, there is a function which is called many times from different places in a program. I want measure how long this function runs in total. Of course, I can use gettimeofday to...
8
by: Joseph | last post by:
I am attempting to create a function that will hide all SPANS within an ided DIV ="idMain", then only display one span, but this function gives an error when it gets to elChildElement as being...
2
by: Bond | last post by:
Hi! I'd like to measure the performance of my web page. I'd like to set a timer on the page to start when a user clicks on a link, or submit... anything where the browser requests a page from the...
6
by: cranium.2003 | last post by:
hello, If i wrote a C function then how to know that its costlier in terms of processing time,execution,compilation. I have written 2 functions for one problem but dont understand which one is...
6
by: Charles M. Reinke | last post by:
I'm using the function clock() to measure the run time of a program so that I can compare among several different algorithms. My code looks like: #include <stdio.h> #include <stdlib.h>...
13
by: Sharon | last post by:
I need to test a performance of some code of mine. I tried using the Environment.TickCount but its resolution is too low (from the MSDN remarks: The resolution of the TickCount property cannot be...
9
by: Steven Bethard | last post by:
I have some text and a list of Element objects and their offsets, e.g.:: ... (etree.Element('a'), 0, 21), ... (etree.Element('b'), 11, 18), ... (etree.Element('c'), 18, 18), ... ] ...
1
by: kartouss | last post by:
Hello i am new to this forum...can you help me solve this problem.. I have a function encrypt() which encrypts data from a file in 16 bytes till end of file which in turn calls other 4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.