如何提高cpu使用率

如何提高cpu使用率,第1张

Windows 9x下是通过读取注册表获取CPU占用,但是只能是整体,不可能细分到每个进程

Private Type LARGE_INTEGER

lowpart As Long

highpart As Long

End Type

Private Declare Function QueryPerformanceCounter Lib _

"kernel32" (lpPerformanceCount As LARGE_INTEGER) _

As Long

Private Declare Function QueryPerformanceFrequency _

Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

Private Const REG_DWORD = 4 ' 32-bit number

Private Const HKEY_DYN_DATA = &H80000006

Private Declare Function RegQueryValueEx Lib _

"advapi32dll" Alias "RegQueryValueExA" (ByVal hKey _

As Long, ByVal lpValueName As String, ByVal _

lpReserved As Long, lpType As Long, lpData _

As Any, lpcbData As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32dll" _

Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal _

lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32dll" _

(ByVal hKey As Long) As Long

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_DblClick()

'If the mouse is double clicked then end the

'program

End

End Sub

Private Sub Form_Load()

'Positions the form at the top right corner of

'the screen regardless of monitor size

Form1Top = 1

Form1Left = ScreenWidth - Form1Width

Call InitCPU

Call OnTop

End Sub

Private Sub SSPanel1_DblClick(Index As Integer)

Call Form_DblClick

End Sub

Private Sub Timer1_Timer()

Dim lData As Long, lType As Long, lSize As Long

Dim hKey As Long

Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StatData", hKey)

'If there's a problem accessing the registry

If Qry <> 0 Then

MsgBox "Can't Open Statistics Key"

End

End If

lType = REG_DWORD

lSize = 4

'Querying the registry for CPUUsage

Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", _

0, lType, lData, lSize)

'clears the SSPanel boxes

Do Until SSPanel1(x)BackColor = &HC0C0C0

SSPanel1(x)BackColor = &HC0C0C0

x = x + 1

If x >= 10 Then Exit Do

Loop

'statbar is the variable that holds the CPU

'usage divided by 10

'(ex if 79% of the CPU is being used then

' statbar will hold the int(79) = 8)

statbar = Int(lData / 10)

If statbar >= 1 Then statbar = statbar - 1

'used to fill the SSPanel with the color green

'beginning with 0 and ending with the value of

'statbar

For fillall = 0 To statbar

SSPanel1(fillall)BackColor = &HFF&

Next fillall

If Int(lData / 10) = 0 Then SSPanel1(0)BackColor = &HC0C0C0

'Print lData

'Label2Caption = lData & "%"

Qry = RegCloseKey(hKey)

End Sub

Private Sub InitCPU()

Dim lData As Long, lType As Long, lSize As Long

Dim hKey As Long

Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StartStat", hKey)

If Qry <> 0 Then

MsgBox "Can't Open Statistics Key"

End

End If

lType = REG_DWORD

lSize = 4

Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", 0, lType, lData, lSize)

Qry = RegCloseKey(hKey)

End Sub

Private Sub OnTop()

Const SWP_NOMOVE = &H2

Const SWP_NOSIZE = &H1

Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Const HWND_TOPMOST = -1

Const HWND_NOTOPMOST = -2

If SetWindowPos(Form1hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS) = True Then

success% = SetWindowPos(Form1hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

End If

End Sub

Windows NT/2000 下

参考一下c的吧,没时间写成VB

主要是使用了Microsoft未公开的 NtQuerySystemInformation

需要ntdlldll

一个VB获取CPU占用率的代码:

>

内存情况比较简单

MEMORYSTATUSEX mstx;

mstxdwLength = sizeof (mstx);

GlobalMemoryStatusEx( &mstx );

int iMemeryUsePercentage = mstxdwMemoryLoad;

int iTotalPhysMB = mstxullTotalPhys/1024/1024;

int iAvailPhysMB = mstxullAvailPhys/1024/1024;

int iTotalPageFileMB = mstxullTotalPageFile/1024/1024;

int iAvailPageFileMB = mstxullAvailPageFile/1024/1024;

char LogBuff[128];

memset( LogBuff , 0 , 128 );

sprintf( LogBuff , "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d" , 100 - iMemeryUsePercentage , iAvailPhysMB , iTotalPhysMB , iAvailPageFileMB , iTotalPageFileMB );

printf("%s\n",LogBuff);

以上程序分别输出可用百分比,可以用物理内存/总物理内存,可用页面文件/总页面文件

获取CPU的比较复杂,我这边只有获取单个进程CPU占用的方法,不过可以遍历所有进程分别获取再求和就是整个cpu占用率了。

#include <stdioh>

#include <Windowsh>

typedef long long int64_t;

typedef unsigned long long uint64_t;

/// 时间转换

static uint64_t file_time_2_utc(const FILETIME ftime)

{

LARGE_INTEGER li;

liLowPart = ftime->dwLowDateTime;

liHighPart = ftime->dwHighDateTime;

return liQuadPart;

}

/// 获得CPU的核数

static int get_processor_number()

{

SYSTEM_INFO info;

GetSystemInfo(&info);

return (int)infodwNumberOfProcessors;

}

int get_cpu_usage(int pid)

{

//cpu数量

static int processor_count_ = -1;

//上一次的时间

static int64_t last_time_ = 0;

static int64_t last_system_time_ = 0;

FILETIME now;

FILETIME creation_time;

FILETIME exit_time;

FILETIME kernel_time;

FILETIME user_time;

int64_t system_time;

int64_t time;

int64_t system_time_delta;

int64_t time_delta;

int cpu = -1;

if(processor_count_ == -1)

{

processor_count_ = get_processor_number();

}

GetSystemTimeAsFileTime(&now);

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))

{

return -1;

}

system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))

/ processor_count_;

time = file_time_2_utc(&now);

if ((last_system_time_ == 0) || (last_time_ == 0))

{

last_system_time_ = system_time;

last_time_ = time;

return -1;

}

system_time_delta = system_time - last_system_time_;

time_delta = time - last_time_;

if (time_delta == 0)

return -1;

cpu = (int)((system_time_delta 100 + time_delta / 2) / time_delta);

last_system_time_ = system_time;

last_time_ = time;

return cpu;

}

int main()

{

while(1)

{

int cpu;

// 参数为进程id

cpu = get_cpu_usage(5160);

printf("CPU使用率: %d%%\n",cpu);

Sleep(1000);

}

return 0;

}

以上就是关于如何提高cpu使用率全部的内容,包括:如何提高cpu使用率、用VC怎么获取CPU使用率、windows系统下sigar如何获取单个进程的cpu使用率求完整代码,我会再多给100财富等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9349071.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存