-1

As I understand from my previous research the resolution timer if we want to measure CPU time of a function is ~15.6ms mean we can get value like 0,15.6,32.2 ms

int a=Process.getCurrentProcess.UserProcessTime;
functionTest();
int b=Process.getCurrentProcess.UserProcessTime;
(b-a) //value like 0,15.6,32.2 ms

But using performance profiler like dotTrace or ant I see in time column where timing option is "CPU Time" value like 4.129; 1.032 ms So it's a high resolution.

What is the method to get this resolution by coding?

functionTest is ==>

    private long FindPrimeNumber(int n)
    {
        int count = 0;
        long a = 2;
        while (count < n)
        {
            long b = 2;
            int prime = 1;// to check if found a prime
            while (b * b <= a)
            {
                if (a % b == 0)
                {
                    prime = 0;
                    break;
                }
                b++;
            }
            if (prime > 0)
                count++;
            a++;
        }
        return (--a);
    }
Omega
  • 1,539
  • 1
  • 11
  • 18
  • If you want to measure short things, use the `Stopwatch` class. It has a much higher resolution. – xanatos Apr 22 '15 at 10:25
  • 1
    @xanatos but stopwatch is wall method i need process method – Omega Apr 22 '15 at 10:46
  • Perhaps they cheat and use both, taking the shortest of the two – xanatos Apr 22 '15 at 10:58
  • Do you *really* need a process method? Are you working in some special environment where the difference matters? – Luaan Apr 22 '15 at 10:59
  • Yeees Because i need to compare performance of a method so if the method take 20ms and i change the code and then it take 15ms it will be good . But the problem is i run the method several time without changing any thing and i get different values 20ms 16ms 13ms so i can't make comparaison then – Omega Apr 22 '15 at 11:02
  • @Omega But that's a case where `Stopwatch` will work fine as well. What's the specific part that can't be solved by using `Stopwatch`? – Luaan Apr 22 '15 at 11:05
  • @luaan StopWatch measure just wall clock time so if other process which has high priority run on process this time will be counted too so it's not what i'm looking for i'm looking exactly the cpu time – Omega Apr 22 '15 at 11:10
  • Well, if you insist that it *is* important for you, you have basically two options - one, run the function enough time to be sure (that's Benchmarking 101) and measure the X calls just once, and divide by X. Two, increase the system-wide timer precision *temporarily*. This is of course a marked waste of cycles, and it will mean less time for your code to execute, but more accurate measurement obviously requires more resources :) – Luaan Apr 22 '15 at 12:05
  • @Luaan For the first one i already did it loop for the function which i'm testing 2000 times and always the average it's different . And for the second one i don't understand how we can increate the system-wide timer precision temporarily . – Omega Apr 22 '15 at 13:11
  • Well, unless you're talking about a pure function, the average is pretty likely to be different - any kind of synchronization, I/O or memory pressure is going to cause the run time to be somewhat undeterministic. If the error is caused by timer resolution, just increase the amount of iterations until the error is negligible (e.g. for a total run time of one second, the max error would be about 16ms, which is 1.5%). As for two, you'll need to do some platform invokes - https://msdn.microsoft.com/en-us/library/windows/desktop/dd757624(v=vs.85).aspx – Luaan Apr 22 '15 at 13:28

1 Answers1

0

It's native methods in WinAPI most likely, you can invoke them from C# via DLLImport. But for simplicity you could try use third party wrapper from here.

But you should clearly understand what you are doing. Between first call of your function and the second one will be difference because of JITting time. And if your method allocates memory - GC might occur any time while calling your method and it will be reflected in measurement.

Alexey Korovin
  • 342
  • 1
  • 8
  • Yes i know i check if their is GC and also i check if it's the second one so let's talk about the value after 10th iteration so with no GC, we will have different values – Omega Apr 22 '15 at 13:12
  • Did you try cycles wrapper? Time of your function may vary due to different reasons. It can use .NET functions that have caches, synchronizations or something else. Nobody knows what your function is doing and how its implemented, so nobody can help you without details. – Alexey Korovin Apr 22 '15 at 14:35
  • you can check the function i upload it – Omega Apr 22 '15 at 16:02
  • @AlexeyKorovin can you give the sample of usage of those class in your code, please? – Andrew_STOP_RU_WAR_IN_UA Sep 19 '21 at 15:44