 |
|
|
 |
|
 |
I have a question on QueryPerformanceCounter.
If I called this both at the beginning and at the end of a code, I can get the elapsed time by:
elapsed time = (end counter - beginning counter) / frequency
This elapsed time is the processing time only for this period of the code spending on the CPU or the time elapsed when the code runs ?
Since there are many other applications running at the same time. Thanks for any comments.
|
|
|
|
 |
|
 |
Thats what I was looking for. thank u very much.
|
|
|
|
 |
|
|
 |
|
 |
it appears to be obsolete in 2.0+ .net (so obsolete for 6yrs now)
The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.
from http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28VS.80%29.aspx
|
|
|
|
 |
|
|
 |
|
 |
i want a timer c# code in which i call a method which is dynamic..and which is to be exicuted every 30 mints
|
|
|
|
 |
|
 |
Very nice. But I have a question.
I would like to create an Event that triggers every so many milliseconds. I only need 1ms accuracy at the most.
Now I’ve tried the easy way with the Clock method. But not matter what I try for the Interval prop, the results are not reliable (1ms Interval gets me 14,15,16ms between the printed time in my log file - 20ms gives me 30,31,32ms).
Your methods is more accurate but only used as a stopwatch. Is there a way to generate an Event using this method? So I can do something simple (write a line to a log file) every so many ms?
Thanks!
Marco
|
|
|
|
 |
|
|
 |
|
 |
you might add a frame timer. Depending on the business, it is called a continuous timer or sample timer, or a time marker. The concept is basically that your end time from one event is the start of another. Although you can add multiple start/stop and duration calls, the extra start command has very small time lost from the stop to the restart.
In continuous mode the stop time is moved to the start after evaluating a marked time duration. Or alternately the previous stop-time is moved to the start prior to calling the stop time. Very handy in evaluating time on continuous events.
I added my own, but thought you might consider it. Thanks for the article!
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
|
|
|
|
 |
|
 |
Hi you!
In .NET,garbage colection is implemented automatically.It can cause your class operating incorrect.
I think you should insert in your class as follow:
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
GC.Collect();//
GC.WaitForPendingFinalizers();//
QueryPerformanceCounter(out startTime);
}
is it ok?
Best Regards
|
|
|
|
 |
|
 |
Hi,
.NET 2.0 now provides this functionality as part of the framework. See class: System.Diagnostics.Stopwatch in System.dll.
Kind Regards,
Mark
|
|
|
|
 |
|
 |
Nice clean code, unlike a lot of the samples you get on Code Project. Two small suggestions:
1) Don't initialize the Int64's to 0, the runtime does that for you.
2) Flag the two native methods with the following attribute:
[return: MarshalAs(UnmanagedType.Bool)]
This brings the code more into line with the CLR specifications, and runs past FxCop without problems.
|
|
|
|
 |
|
 |
Hi,
There a couple of gotcha's you have to watch out for when using Query... API.
First, and most important is that on some platforms (namely mobile platforms, but also some servers too), the frequecy can change dynamically during executation of your code. If this happens, then you'll need to take this into account in more complicated way.
Second, is the question of Multi-core. Your thread may begin timing on one core and then finish on another core. If you are using the classic RDTSC method (Read TimeStamp) then you can be in big trouble because the clocks between cores are not synchronized and definitely start at different times.
QueryPerformanceCounter is supposed to take this into account. However if you read the fine print in MSDN, there seems to suggest that this method is not guaranteed either.
In general, when I'm trying to get precise measurements, I will use SetThreadAffinity (don't remember the C# equivalent off top of my head) to tie the thread to a single core and then measure. If you have multiple threads involved in the part you are timing, then well it gets more complicated....
Aaron
|
|
|
|
 |
|
 |
Hi,
I like your article, but I wanted to point out a problem that will help you make it even better. There is no such thing as an exact measurement of time. Exactness or accuracy of a measurement is the degree of conformity with a standard (the "truth"). The difference between the measurement and the standard represents the error. Precision relates to the consistency of which a measurement may be reproduced and to the number of significant digits a measurement may reliably be made. Just because you can reliably measure something out to 10 decimal places doesn't mean that it is accurate (or exact) out to 10 decimal places.
So, it is more correct to say that your application provides a more precise measurement of time.
Gary Kirkham
Forever Forgiven and Alive in the Spirit
He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot
Me blog, You read
|
|
|
|
 |
|
 |
Gary,
Could you please point out in this context how is the QueryPerformanceCounter not "ACCURATE" and how to remedy this problem!!!
Thanks
|
|
|
|
 |
|
 |
get the occasional 0 or 0.0000000000000000001 but that's OK, timing is tricky.
Thanks for the class, saved me some time )
|
|
|
|
 |
|
 |
I was initially puzzled by the following code -- especially the comment!
// Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
}
It seems to me that the real reason to call Sleep(0) here is simply to resume this
thread's execution in a fresh time-slice, to reduce the probability it will be suspended
during the measurement. (Of course, the scheduler may have different ideas!) So let me
suggest the following clarification:
// Start the timer
public void Start()
{
// abandon any remaining time in this slice
Thread.Sleep(0);
// resume in a fresh time slice (if you please, Mr. Scheduler!)
QueryPerformanceCounter(out startTime);
}
Nevertheless, the Sleep(0) call is a reasonable thing to do there. One could also
increase the thread's .Priority to (perhaps) further ward off a premature suspension.
Again, the Windows scheduler may not cooperate.
Performance measurement is always a trick, without hardware assist!
DrGary
|
|
|
|
 |
|
|
 |
|
 |
Dear Sir,
Iam a new progrmmer and I need this code to measure time for some process with high precision, and iam using Visual C++ 6,
and when i copy this code and paste it in the Visual C++ and compile the code it gives errors which iam pasting now:
Can you please tell me which mistake am i making
thanks
Compiling...
Text1.c
D:\USER\Perkash\Text1.c(1) : error C2061: syntax error : identifier 'System'
D:\USER\Perkash\Text1.c(1) : error C2059: syntax error : ';'
D:\USER\Perkash\Text1.c(2) : error C2061: syntax error : identifier 'System'
D:\USER\Perkash\Text1.c(2) : error C2059: syntax error : ';'
D:\USER\Perkash\Text1.c(2) : error C2059: syntax error : '.'
D:\USER\Perkash\Text1.c(3) : error C2061: syntax error : identifier 'System'
D:\USER\Perkash\Text1.c(3) : error C2059: syntax error : ';'
D:\USER\Perkash\Text1.c(3) : error C2059: syntax error : '.'
D:\USER\Perkash\Text1.c(4) : error C2061: syntax error : identifier 'System'
D:\USER\Perkash\Text1.c(4) : error C2059: syntax error : ';'
D:\USER\Perkash\Text1.c(4) : error C2059: syntax error : '.'
D:\USER\Perkash\Text1.c(6) : error C2061: syntax error : identifier 'Win32'
D:\USER\Perkash\Text1.c(6) : error C2059: syntax error : ';'
D:\USER\Perkash\Text1.c(7) : error C2449: found '{' at file scope (missing function header?)
D:\USER\Perkash\Text1.c(58) : error C2059: syntax error : '}'
Error executing cl.exe.
Text1.exe - 15 error(s), 0 warning(s)
with regards
Perkash
|
|
|
|
 |
|
 |
Probably too late to be helpfull. The problem you are having is that this example is written in C# and your trying to write it in C++. I do not know how you would make a similar functionality in C++, but I am sure it can be converted.
|
|
|
|
 |
|
 |
Instead of using this code, just tap into the API directly. You can't use this code. It's not the right programming language.
ROFLOLMFAO
|
|
|
|
 |
|
 |
At least according to the following KB article:
http://support.microsoft.com/kb/306979/EN-US/[^]
[DllImport("kernel32.dll")]
extern static short QueryPerformanceCounter(ref long counter);
[DllImport("kernel32.dll")]
extern static short QueryPerformanceFrequency(ref long frequency);
|
|
|
|
 |
|
 |
The KB article is less accurate than the code provided in the article above.
If you use the KB code, you'll need to initialize the variables you pass to the API functions (by CLR verification rules), and what's more you'll be required to check for non-zero / zero for success / fail, while P/Invoke can do this automatically.
It's better to use the functions in the article above.
|
|
|
|
 |
|
 |
Anonymous wrote:
The KB article is less accurate than the code provided in the article above.
I think it's better to cite some source and reference links when you attempt to contradict a Microsoft support article about how to call a Microsoft API function from the Microsoft .NET framework.
|
|
|
|
 |