Get # of cores?

Is there a way to determine the number of cores on desktop machines Xplatform? (well at least Mac and Windows)

I considering writing an app that would use helpers for the first time, and knowing how to determine the # of cores would help determine how many helpers to spawn.


  • Karen

Check SystemInformationMBS module in MBS Xojo Util Plugin.
There is a processor count function.

[quote=458033:@Christian Schmitz]Check SystemInformationMBS module in MBS Xojo Util Plugin.
There is a processor count function.[/quote]

My last License is from 2014 and in any case I don’t want to use plugins for this potential project. Hoping there are some declares I can use.

  • Karen

You can do it with a shell command.

On Mac:

sysctl -n hw.ncpu

On Windows:

WMIC CPU Get DeviceID,NumberOfCores,NumberOfLogicalProcessors


Trying that out made me realize things are not as simple as I was was thinking.

I have a 4 core cpu in my 2013 iMac (Core i7) which supports hyper-threading… As such that command reports 8 cores…

On a PC it looks like you get both actual and virtual cores,

In terms of performance, should I really consider it to be 8 cores for determining the max number of helpers to spawn, or will having more than one helper per physical core likely to bog down the machine? (Before considering hyper threading i was thinking I should always make sure I leave at least one physical core unused by my app)

If it matters, is there a way to determine the actual number of physical cores on a Mac?


  • Karen

sysctl -n hw.physicalcpu

sysctl -n hw.logicalcpu

Thanks you both… your combined answer answers my questions…
I would rather use declares than shell out (less overhead and perhaps more stable long term) but this works!

[quote=458052:@Walter Purvis]On Windows:

WMIC CPU Get DeviceID,NumberOfCores,NumberOfLogicalProcessors[/quote]

For macOS:

[quote=458057:@Tiago Ribeiro]sysctl -n hw.physicalcpu

sysctl -n hw.logicalcpu


You could limit it to “cores - 1” or “cores - 2” if you want to leave resources for other things, but I probably wouldn’t worry about it.

The overhead of a shell command is very, very small, considering you only have to determine the number of cores one time, at application open. As for stability, I doubt shell commands change any more frequently than OS APIs; some shell commands haven’t changed since the 70s.