如何检测iOS上的双核CPU?

我的应用程序使用NSOperationQueue在后台线程中caching缩略图。 在iPad2上,我可以将并发任务数量限制提高到5或6,但是在iPad 1这样的单核设备上,这会使用户界面陷入停顿。

所以,我想检测双核心设备(目前只有iPad 2),并适当调整并发限制。 我知道我不应该检查型号,而是设备function。 那么,我应该寻找什么设备function,这将告诉我,该CPU是双核心?

方法1

 [[NSProcessInfo processInfo] activeProcessorCount]; 

NSProcessInfo也有一个processorCount属性。 了解这里的区别。

方法2

 #include <mach/mach_host.h> unsigned int countCores() { host_basic_info_data_t hostInfo; mach_msg_type_number_t infoCount; infoCount = HOST_BASIC_INFO_COUNT; host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount ) ; return (unsigned int)(hostInfo.max_cpus); } 

方法3

 #include <sys/sysctl.h> unsigned int countCores() { size_t len; unsigned int ncpu; len = sizeof(ncpu); sysctlbyname ("hw.ncpu",&ncpu,&len,NULL,0); return ncpu; } 

只要使用:

[[NSProcessInfo processInfo] processorCount]

除此之外,您可以通过系统调用获取该信息…

 NSDictionary dictionaryWithObjectsAndKeys: [self machineType],@"MachineType", [self humanMachineType],@"HumanMachineType", [self powerPCTypeString],@"ProcessorType", [NSNumber numberWithLong: [self processorClockSpeed]], @"ProcessorClockSpeed", [NSNumber numberWithLong: [self processorClockSpeedInMHz]], @"ProcessorClockSpeedInMHz", [NSNumber numberWithInt:[self countProcessors]], @"CountProcessors", [self computerName],@"ComputerName", [self computerSerialNumber],@"ComputerSerialNumber", [self operatingSystemString],@"OperatingSystem", [self systemVersionString],@"SystemVersion", nil]; 

这里是参考… http://cocoadev.com/HowToGetHardwareAndNetworkInfo

我想这需要一个单独的答案,而不是一个评论:

我想知道核心的数量意味着现在苹果带来更多的不对称处理与A10融合? 我们有2.2核心还是4核心? 苹果忽略了使activeProcessorCount浮动来说明它的分形性质。 请注意,即使在融合之前,情况也是如此,因为那时候他们可能会受到热量的限制。 他们需要修复nsoperationqueue的overcomittal,或者拿出一个相当于float的activeProcessorCount,并且反对当前forms的activeProcessorCount,在最近的hw进展中失去了它的效用

所以,摩尔定律越快被遗忘或接受一个新的常数越无意义的核心计数。 除非你正在编写一个怪胎长椅42,多核评分版。

生活在2016年年底,为了解决你所面临的潜在问题,而不是通过最大并发操作支持攻击,我假设你已经调整了NSOperationQueue的QoS到.Background?

我认为这是一个更清洁的方式来解决您的问题与现代ios sdk然后计数使用英里的绳索albertamg

另外请看看NSProcessInfoThermalStateDidChangeNotification(macos)和NSProcessInfo.lowPowerModeEnabled(ios)(我猜想另一种观察NSProcessInfoThermalStateDidChangeNotification就是对KVo的activeProcessorCount新值)

如果你开始考虑这些新的现实,魔法常数调整倍增的核心数将非常快速地变得有趣……并且随着新硬件从cupertino中滚出而变得腐烂。

在苹果hw的当前动物园里,为了让套接字级别的networking正常工作,这将是一件容易的事情:可行,但是通过在Cupetino中select几个,并且在检查质量的肩膀上隐藏着iSteve阴影;-)

我猜:

 sysctl(HW_NCPU) 

要么

 sysctlbyname("hw.ncpu", NULL, &size, NULL, 0); 

应该pipe用。

您可以通过以下方式修改代码: 检测特定的iPhone / iPod touch型号