System.Diagnostics.PerformanceCounter NextValue(): why so slow?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi All,<br/><br/>Im finding that calling NextValue() on a PerformanceCounter is extremely slow.  I say slow, because when I add the exact same counters (as my code uses) to perfmon, the sampling takes place every second without issue.  To sample the same set of counters (instances of the Thread/Processor Time counter), my code takes 40 seconds+.<br/><br/>My code, in its simplest form, is doing this:<br/><br/>
<div style="color:black;background-color:white
<pre> <span style="color:blue private <span style="color:blue void Foo()

{

PerformanceCounter c = <span style="color:blue new PerformanceCounter();



c.MachineName = <span style="color:#a31515 "AServer";

c.CategoryName = <span style="color:#a31515 "Thread";

c.CounterName = <span style="color:#a31515 "% Processor Time";



<span style="color:blue foreach (instance k <span style="color:blue in col) <span style="color:green //about 60ish...

{

c.InstanceName = k.InstanceName;

c.NextValue(); <span style="color:green //is assigned to simple var

}

}

[/code]

<br/>(And yes, I realise I should be calling NextValue twice initially).<br/><br/>As I mentioned, the same set of 60ish counters in perfmon sample quite happily.  Am I doing something stupid, or is there a quicker way to "batch" sample instances?<br/><br/>Thanks,<br/>Rob<br/>

View the full article
 
Back
Top