Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect CPU cores on iOS device [duplicate]

I am writing some code with dispatch_async and get different results on an iphone 4s and ipad 1st gen.

I am wondering if it is due to the number of cores the CPU has. Is it possible to detect the number of cores or CPU type of an iOS device at runtime so I can dispatch_async on the 4s, but not on the ipad?

like image 519
some_id Avatar asked Oct 11 '12 12:10

some_id


1 Answers

Here's the code to detect the number of cores on an iOS device:

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

In addition to that, you can check against the [[UIDevice currentDevice] userInterfaceIdiom] to determine if the device is an iPhone or an iPad. Like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    NSLog(@"iPad");
}
else {
    NSLog(@"iPhone");
}

Reference

like image 184
Simon Germain Avatar answered Oct 14 '22 22:10

Simon Germain