damian damian - 2 months ago 20
iOS Question

How do I detect a dual core CPU on iOS?

My app uses an

NSOperationQueue
to cache thumbnail images in a background thread. On the iPad2 I can push the concurrent task count limit up to 5 or 6, but on single core devices like the iPad 1 this brings the UI to a grinding halt.

So, I'd like to detect a dual core device (currently only iPad 2) and adapt the concurrent limit appropriately. I know I'm not supposed to check model numbers, rather device features. So what device feature should I be looking for that would tell me whether the cpu is dual core?

Answer

Method 1

[[NSProcessInfo processInfo] activeProcessorCount];

NSProcessInfo also has a processorCount property. Learn the difference here.

Method 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);
}

Method 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;
}