我正在编写这个类来监控通过所有网络接口的带宽。尽管整个过程运行得很好,但我还是想尝试减少查询过程中的CPU使用率。原因是我每隔几秒钟就运行一次,每次都会增加大约7-8%的CPU使用率。有什么方法可以减少CPU周期的数量吗?
PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
String[] instancename = category.GetInstanceNames();
foreach (string name in instancename)
{
String networkCard = name;
const int numberOfIterations = 10;
PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);
float bandwidth = bandwidthCounter.NextValue();
PerformanceCounter dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);
PerformanceCounter dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);
float sendSum = 0;
float receiveSum = 0;
for (int index = 0; index < numberOfIterations; index++)
{
sendSum += dataSentCounter.NextValue();
receiveSum += dataReceivedCounter.NextValue();
}
float dataSent = sendSum;
float dataReceived = receiveSum;
double dout = (((8 * (dataSent)) / (bandwidth * numberOfIterations) * 100) * 1024) / 100;
double din = (((8 * (dataReceived)) / (bandwidth * numberOfIterations) * 100) * 1024) / 100;
dout = Math.Round(dout, 2);
din = Math.Round(din, 2);
string doutround = Convert.ToString(dout);
string dinround = Convert.ToString(din);
String output = dinround + "/" + doutround;
GlobalsWMI.NetIOs.Add(output);
}
String tooutput = String.Join("AND", GlobalsWMI.NetIOs);
GlobalsWMI.NetIOs.Clear();
return tooutput;发布于 2013-06-05 22:18:36
好的,我已经尝试了很多方法,我认为这个版本要快得多:
private string GetNetworkUsage(System.Net.NetworkInformation.NetworkInterface[] nis)
{
foreach (NetworkInterface ni in nis)
{
float bandwidth = ni.Speed;
const int numberOfIterations = 10;
float sendSum = 0;
float receiveSum = 0;
float lastRec = ni.GetIPv4Statistics().BytesReceived;
float lastSent = ni.GetIPv4Statistics().BytesSent;
for (int index = 0; index < numberOfIterations; index++)
{
System.Threading.Thread.Sleep(10);
float br = ni.GetIPv4Statistics().BytesReceived;
float bs = ni.GetIPv4Statistics().BytesSent;
receiveSum += br - lastRec;
sendSum += bs - lastSent;
lastRec = br;
lastSent = bs;
}
float dataSent = sendSum;
float dataReceived = receiveSum;
double dout = (((8 * (dataSent)) / (bandwidth) * 100) * 1024) / 100;
double din = (((8 * (dataReceived)) / (bandwidth) * 100) * 1024) / 100;
dout = Math.Round(dout, 2);
din = Math.Round(din, 2);
string doutround = Convert.ToString(dout);
string dinround = Convert.ToString(din);
String output = dinround + "/" + doutround;
GlobalsWMI.NetIOs.Add(output);
}
String tooutput = String.Join("AND", GlobalsWMI.NetIOs);
GlobalsWMI.NetIOs.Clear();
return tooutput;
}这样叫它:
System.Net.NetworkInformation.NetworkInterface[] nis = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
// this is you for loop add you sleep or however you do it here
for (int i = 0; i < int.MaxValue; i++)
{
Console.WriteLine(test(nis));
}在我的系统上试过了,它使用的CPU更少,速度也更快,但我会仔细检查这些数字是否符合您的预期。
发布于 2013-06-05 21:18:51
随着时间的推移,您可以使用System.Threading.Thread.Sleep(x)在某些点上减少CPU使用率-您的检查将持续更长时间,但平均CPU使用率较低。
https://stackoverflow.com/questions/16940913
复制相似问题