2

I'm using TotalProcessorTime to mesure time speding by a method in cpu.

for(int i=0;i<10;i++){
    double s = Process.getCurrentProcess().TotalProcessorTime;
    primeNumber(500);  //return the 500 primenumber 
    doube e = Process.getCurrentProcess().TotalProcessorTime;
    Console.writeLine(e-s);
}

the output are like: 0 15,0001 15,0001 0 32,0002 0 15,0001 0 15,0001

So All output are mutiliple to 15,6001 the resolution is so hight.

So How we can reduce this resolution to have values like 1,2,6,5,2 ?

And why for the same code it give different values 0 15,6001 32,0002 ?

Omega
  • 1,539
  • 1
  • 11
  • 18
  • 1
    Are you sure you need to take time via `TotalProcessorTime`? Why don't you just use a [Stopwatch](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.100%29.aspx)? – germi Apr 16 '15 at 13:51
  • 1
    This timer is based on the clock interrupt frequency, by default it ticks 64 times per second. You can get a *much* higher resolution, that requires pinvoking QueryProcessCycleTime(). Albeit that it returns processor cycles, not time. – Hans Passant Apr 16 '15 at 13:55
  • @germi stopwatch will take a time also for others backgrounds process like if it's processor is not with our process this time will be calculated too (Schedulling stuff). So i need just the time when my process is in cpu – Omega Apr 16 '15 at 14:03
  • @Sayse don't understand can u explain more ? – Omega Apr 16 '15 at 14:04
  • @HansPassant how i can use QueryProcessCycleTime() for fixing the problem with TotalProcessTime to get only the time when the process is in the cpu ? – Omega Apr 16 '15 at 14:05
  • Well, that's exactly what it returns, the total number processor cycles consumed by the process when it was "in the cpu". You can't possibly get a better measurement, a processor cycle is the smallest possible unit of measurement. It is not clear to me why that isn't good enough. – Hans Passant Apr 16 '15 at 14:10
  • @Omega My question was more about if you *really* need that kind of precision and accuracy. – germi Apr 16 '15 at 14:20
  • So The number of ticks which it return is 156001 tick it's bad resolution I'm wondering if their a way to reduce this resolution and we can have then value like 100 200 10000 ticks – Omega Apr 16 '15 at 14:21
  • @germi Yes I need It – Omega Apr 16 '15 at 14:22
  • @HansPassant: QueryProcessCycleTime: _This function uses timer services provided by the CPU, which can vary in implementation. For example, some CPUs will vary the frequency of the timer when changing the frequency at which the CPU runs and others will leave it at a fixed rate._ – Arno Apr 16 '15 at 14:47
  • Well, obviously, that's why I mentioned that it returns cycles, not time. – Hans Passant Apr 16 '15 at 14:49
  • @HansPassant If we know the number of cycle and the frequency so we will know the time it's not problem if u mean with the cycle is the tick so the resolution of TotalProcessorTime is so bad 156001 Tick if we use TotalProcessorTime.Ticks – Omega Apr 16 '15 at 14:56

1 Answers1

1

I am almost certain you would want to use Stopwatch, QueryPerformanceCounter, or alike for what you desire. However, the granularity of TotalProcessorTime can be modified:

The granularity/resolution is determined by the systems timer resolution. This Accurate Windows timer?... answer shows how to obtain and how to modify the systems timer resolution. Coding examples are given here (c) and here (c#).

Community
  • 1
  • 1
Arno
  • 4,994
  • 3
  • 39
  • 63
  • I wasn't attemting to provide code for your purpose. I wanted to give you a clue. However, you may look [here](http://stackoverflow.com/a/15071477/1504523) – Arno Apr 16 '15 at 15:01
  • I told u that Because i already see the link which u gave me i tested the code but didn't work same thing :) – Omega Apr 16 '15 at 15:07