dbIdleTime = 100.0 - dbIdleTime * 100.0/(double)SysBaseInfo.bKeNumberProcessors + 0.5;
看到计算CPU占用率的算法,为何要加0.5呢?是什么意思?
麻烦各位指导一下,谢谢
附:
"java"
inline int CCpuUsage::OnTimer()
{
status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
return 0;
// get new CPU's idle time
status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
return 0;
// if it's a first call - skip it
if (liOldIdleTime.QuadPart != 0)
{
// CurrentValue = NewValue - OldValue
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime = dbIdleTime / dbSystemTime;
// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime = 100.0 - dbIdleTime * 100.0/(double)SysBaseInfo.bKeNumberProcessors + 0.5;
m_nCpuUsage = (UINT)dbIdleTime;
}
// store new CPU's idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSystemTime = SysTimeInfo.liKeSystemTime;
// wait one second
return 0;
}
6 个解决方案
#1
是不是进整
#2
两个一样的帖子
#3
任务管理器本省的运行也会占用部分CPU。。。
#4
LZ准备面试微软?
#5
貌似《编程之美》上面有讲解
#6
多谢楼上各位帮忙,正如上面所说,的确是四舍五入。。。
#1
是不是进整
#2
两个一样的帖子
#3
任务管理器本省的运行也会占用部分CPU。。。
#4
LZ准备面试微软?
#5
貌似《编程之美》上面有讲解
#6
多谢楼上各位帮忙,正如上面所说,的确是四舍五入。。。