寻找Win32 API函数,C++或Delphi示例代码,它告诉我线程的CPU使用率(百分比和/或总CPU时间)(而不是进程的总和).我有线程ID.
我知道Sysinternals Process Explorer可以显示这些信息,但我在程序中需要这些信息.
我感兴趣的是如何把它放在循环中,以便获得cpu执行每个不同操作的实时时间
#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
typedef unsigned __int64 uint64;
const uint64 m1=0x5555555555555555;
const uint64 m2=0x3333333333333333;
const uint64 m4=0x0f0f0f0f0f0f0f0f;
const uint64 m8=0x00ff00ff00ff00ff;
const uint64 m16=0x0000ffff0000ffff;
const uint64 m32=0x00000000ffffffff;
const uint64 hff=0xffffffffffffffff;
const uint64 h01=0x0101010101010101;
uint64 popcount_1(uint64 x)
{
x=(x&m1)+((x>>1)&m1);
x=(x&m2)+((x>>2)&m2);
x=(x&m4)+((x>>4)&m4);
x=(x&m8)+((x>>8)&m8);
x=(x&m16)+((x>>16)&m16);
x=(x&m32)+((x>>32)&m32);
return (uint64)x;
}
//This uses fewer arithmetic operations than any other known
//implementation on machines with slow multiplication.
//It uses 17 arithmetic operations.
int popcount_2(uint64 x)
{
x-=(x>>1)&m1;//put count of each 2 bits into those …Run Code Online (Sandbox Code Playgroud)