Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

c# - How we can reduce the resolution of myProcess.TotalProcessorTime?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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#).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...