本文介绍了如何有效地检测逻辑和物理处理器的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前我正在使用此功能。它工作正常,但每个查询大约需要1秒钟。因此,就我而言,我在应用程序中浪费了3秒。目前,我正在考虑使用3个线程在一秒钟内获取所有信息。
Currently I'm using this function. It works fine but each query takes about 1 second. So in my case I waste 3 seconds in my application. At the moment I'm thinking about using 3 threads to get this all information in one second.
function GetWMIstring (wmiHost, wmiClass, wmiProperty : string):string;
var // These are all needed for the WMI querying process
Locator: ISWbemLocator;
Services: ISWbemServices;
SObject: ISWbemObject;
ObjSet: ISWbemObjectSet;
SProp: ISWbemProperty;
Enum: IEnumVariant;
Value: Cardinal;
TempObj: OleVariant;
SN: string;
begin
Result := '';
try
Locator := CoSWbemLocator.Create; // Create the Location object
// Connect to the WMI service, with the root\cimv2 namespace
Services := Locator.ConnectServer(wmiHost, 'root\cimv2', '', '', '','', 0, nil);
ObjSet := Services.ExecQuery('SELECT * FROM '+wmiClass, 'WQL',
wbemFlagReturnImmediately and wbemFlagForwardOnly , nil);
Enum := (ObjSet._NewEnum) as IEnumVariant;
while Enum.Next(1, TempObj, Value) = S_OK do
begin
try SObject := IUnknown(TempObj) as ISWBemObject; except SObject := nil; end;
TempObj := Unassigned; // Always need to free interface in TempObj
if SObject <> nil then
begin
SProp := SObject.Properties_.Item(wmiProperty, 0);
SN := SProp.Get_Value;
if not VarIsNull(SN) then
begin
Result := SN;
Break;
end;
end;
end;
except // Trap any exceptions (Not having WMI installed will cause one!)
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CoInitializeEx(nil,COINIT_MULTITHREADED) ; //required !!! Otherwise "EOleSysError: CoInitialize has not been called" occurs (Microsoft recommends CoInitializeEx instead of CoInitialize)
CPU_PROCESSOR_COUNT := StrToInt( getWMIstring('','Win32_ComputerSystem','NumberOfProcessors') ); // number of cpu on mainboard
CPU_PHYSICAL_CORES := StrToInt( getWMIstring('','Win32_Processor','NumberOfCores') ); // number of phisical cores
CPU_LOGICAL_CORES := StrToInt( getWMIstring('','Win32_Processor','NumberOfLogicalProcessors') ); //number of logical cores
CoUninitialize; //required !!!
end;
推荐答案
如果您对逻辑处理器的数量感兴趣-出于线程调度的目的,要容易得多且更早 函数:
If you're interested in the number of logical processors - say for thread scheduling purposes, there is much easier and earlier GetSystemInfo function:
function GetNumberOfProcessors: Integer;
var
si: TSystemInfo; //Windows.pas
begin
GetSystemInfo({var}si);
Result := si.dwNumberOfProcessors;
end;
几乎所有目的:
- 它们是单独的物理CPU
- 还是它们是核心
- 或每个物理CPU都没关系
- 或如果它们正在超线程虚拟内核
- it doesn't matter if they are individual physical CPUs
- or if they're cores
- or multiple physical CPUs each with multiple cores
- or if they're hyperthreading virtual cores
您想要什么 GetSystemInfo 提供。
这篇关于如何有效地检测逻辑和物理处理器的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!