不多废话,直接展示代码
代码语言:javascript代码运行次数:0运行复制```javascript function info = cpuinfo() % CPU数据采集 % 信息 = CPUINFO()返回一个包含不同字段的结构 % 中央处理器和操作系统信息由/proc/cpuinfo(Unix)、 % sysctl(Mac)或WMIC(Windows)提供。 % CPU名称 % CPU的时钟频率 % CPU缓存大小(L2) % 物理CPU核心数 % * 操作系统名称和版本 % % 参见: computer, isunix, ismac
if isunix % 判断是否为UNIX系统 if ismac % 判断是否为Mac系统 info = cpuInfoMac(); else info = cpuInfoUnix(); end else info = cpuInfoWindows(); end
%------------------------------------------------------------------------- function info = cpuInfoWindows() sysInfo = callWMIC('cpu'); osInfo = callWMIC('os'); info = struct( ... 'Name', sysInfo.Name, ... 'Clock', [sysInfo.MaxClockSpeed, ' MHz'], ... 'Cache', [sysInfo.L2CacheSize, ' KB'], ... 'NumProcessors', str2double(sysInfo.NumberOfCores), ... 'OSType', 'Windows', ... 'OSVersion', osInfo.Caption); % 输出结构
%------------------------------------------------------------------------- function info = callWMIC(alias) % 调用Windows管理命令行WMIC olddir = pwd(); % 保存当前目录 cd(tempdir); % 切换到临时目录 sysinfo = evalc(sprintf('!wmic %s get /value', alias)); % 执行WMIC命令 cd(olddir); % 切换回原目录 fields = textscan(sysinfo, '%s', 'Delimiter', '\n'); fields = fields{1}; % 处理数据 fields(cellfun('isempty', fields)) = []; % 去除空行 values = cell(size(fields)); for ff = 1:numel(fields) idx = find(fields{ff} == '=', 1, 'first'); % 查找等号 if ~isempty(idx) && idx > 1 values{ff} = strtrim(fields{ff}(idx+1:end)); % 提取值 fields{ff} = strtrim(fields{ff}(1:idx-1)); % 提取字段名 end end % 删除任何重复项(仅适用于双插槽电脑,我们假设所有插槽有相同的处理器)。 numResults = sum(strcmpi(fields, fields{1})); if numResults > 1 % 计算核心数 numCoresEntries = find(strcmpi(fields, 'NumberOfCores')); if ~isempty(numCoresEntries) cores = cellfun(@str2double, values(numCoresEntries)); values(numCoresEntries) = {num2str(sum(cores))}; end % 去除重复结果 [fields, idx] = unique(fields, 'first'); values = values(idx); end % 数据转换 info = cell2struct(values, fields);
%------------------------------------------------------------------------- function info = cpuInfoMac() % Mac系统与Windows类似 machdep = callSysCtl('machdep.cpu'); hw = callSysCtl('hw'); info = struct( ... 'Name', machdep.brand_string, ... 'Clock', [num2str(str2double(hw.cpufrequency_max)/1e6), ' MHz'], ... 'Cache', [machdep.cache.size, ' KB'], ... 'NumProcessors', str2double(machdep.core_count), ... 'OSType', 'Mac OS/X', ... 'OSVersion', getOSXVersion());
%------------------------------------------------------------------------- function info = callSysCtl(namespace) infostr = evalc(sprintf('!sysctl -a %s', namespace)); % 移除前缀 infostr = strrep(infostr, [namespace, '.'], ''); % 现在转换为结构 infostr = textscan(infostr, '%s', 'delimiter', '\n'); infostr = infostr{1}; info = struct(); for ii = 1:numel(infostr) colonIdx = find(infostr{ii} == ':', 1, 'first'); if isempty(colonIdx) || colonIdx == 1 || colonIdx == length(infostr{ii}) continue end prefix = infostr{ii}(1:colonIdx-1); value = strtrim(infostr{ii}(colonIdx+1:end)); while ismember('.', prefix) dotIndex = find(prefix == '.', 1, 'last'); suffix = prefix(dotIndex+1:end); prefix = prefix(1:dotIndex-1); value = struct(suffix, value); end info.(prefix) = value; end
%------------------------------------------------------------------------- function vernum = getOSXVersion() % 提取操作系统版本号 ver = evalc('system(''sw_vers'')'); vernum = regexp(ver, 'ProductVersion:\s([1234567890.]*)', 'tokens', 'once'); vernum = strtrim(vernum{1});
%------------------------------------------------------------------------- function info = cpuInfoUnix() txt = readCPUInfo(); cpuinfo = parseCPUInfoText(txt); txt = readOSInfo(); osinfo = parseOSInfoText(txt); % 合并结构 info = cell2struct([struct2cell(cpuinfo); struct2cell(osinfo)], ... [fieldnames(cpuinfo); fieldnames(osinfo)]);
%------------------------------------------------------------------------- function info = parseCPUInfoText(txt) % 解析字段 lookup = { ... 'model name', 'Name' ... 'cpu Mhz', 'Clock' ... 'cpu cores', 'NumProcessors' ... 'cache size', 'Cache' ... }; info = struct( ... 'Name', {''}, ... 'Clock', {''}, ... 'Cache', {''} ); for ii = 1:numel(txt) if isempty(txt{ii}) continue; end % 查找分隔属性名和值的冒号 colon = find(txt{ii} == ':', 1, 'first'); if isempty(colon) || colon == 1 || colon == length(txt{ii}) continue; end fieldName = strtrim(txt{ii}(1:colon-1)); fieldValue = strtrim(txt{ii}(colon+1:end)); if isempty(fieldName) || isempty(fieldValue) continue; end % 是否为我们感兴趣的字段之一? idx = find(strcmpi(lookup(:,1), fieldName)); if ~isempty(idx) newName = lookup{idx,2}; info.(newName) = fieldValue; end end % 转换时钟速度 info.Clock = [info.Clock, ' MHz']; % 转换核心数 info.NumProcessors = str2double(info.NumProcessors);
%------------------------------------------------------------------------- function info = parseOSInfoText(txt) info = struct( ... 'OSType', 'Linux', ... 'OSVersion', ''); % 查找字符串"linux version"然后查找括号内的内容 [~, b] = regexp(txt, '[^(](([^)])).*', 'match', 'tokens', 'once'); info.OSVersion = b{1}{1};
%------------------------------------------------------------------------- function txt = readCPUInfo() fid = fopen('/proc/cpuinfo', 'rt'); if fid
输出结果如下
代码语言:javascript代码运行次数:0<svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M6.66666 10.9999L10.6667 7.99992L6.66666 4.99992V10.9999ZM7.99999 1.33325C4.31999 1.33325 1.33333 4.31992 1.33333 7.99992C1.33333 11.6799 4.31999 14.6666 7.99999 14.6666C11.68 14.6666 14.6667 11.6799 14.6667 7.99992C14.6667 4.31992 11.68 1.33325 7.99999 1.33325ZM7.99999 13.3333C5.05999 13.3333 2.66666 10.9399 2.66666 7.99992C2.66666 5.05992 5.05999 2.66659 7.99999 2.66659C10.94 2.66659 13.3333 5.05992 13.3333 7.99992C13.3333 10.9399 10.94 13.3333 7.99999 13.3333Z" fill="currentcolor"></path></svg>运行<svg fill="none" height="16" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4.5 15.5V3.5H14.5V15.5H4.5ZM12.5 5.5H6.5V13.5H12.5V5.5ZM9.5 2.5H3.5V12.5H1.5V0.5H11.5V2.5H9.5Z" fill="currentcolor" fill-rule="evenodd"></path></svg>复制```javascript
>> info = cpuinfo()
info =
包含以下字段的 struct:
Name: '12th Gen Intel(R) Core(TM) i5-12500H'
Clock: '2500 MHz'
Cache: '9216 KB'
NumProcessors: 12
OSType: 'Windows'
OSVersion: 'Microsoft Windows 11 家庭中文版'