user$ sysctl -n hw.ncpu8
According to Wikipedia, (http://en.wikipedia.org/wiki/Intel_Core#Core_i7) there is no Core i7 with 8 physical cores so the Hyperthreading idea must be the case. Ignore sysctl
and use the system_profiler
value for accuracy. The real question is whether or not you can efficiently run applications with 4 cores (long compile jobs?) without interrupting other processes.
Running a compiler parallelized with 4 cores doesn't appear to dramatically affect regular OS operations. So perhaps treating it as 8 cores is not so bad.
As jkp said in a comment, that doesn't show the actual number of physical cores. to get the number of physical cores you can use the following command:
system_profiler SPHardwareDataType
It wasn't specified in the original question (although I saw OP post in comments that this wasn't an option), but many developers on macOS have the Homebrew package manager installed.
For future developers who stumble upon this question, as long as the assumption (or requirement) of Homebrew being installed exists (e.g., in an engineering organization in a company), nproc
is one of the common GNU binaries that is included in the coreutils
package.
brew install coreutils
If you have scripts that you would prefer to write once (for Linux + macOS) instead of twice, or to avoid having if
blocks where you need to detect the OS to know whether or not to call nproc
vs sysctl -n hw.logicalcpu
, this may be a better option.
The following command gives you all information about your CPU
$ sysctl -a | sort | grep cpu
Comments for 2 good replies above:
1) re the accepted reply (and comments) by jkp: hw.ncpu is apparently deprecated in favor of hw.logicalcpu (https://ghc.haskell.org/trac/ghc/ticket/8594)
2) re the 2014 update by Karl Ehr: on my computer (with 2.5 ghz intel core i7),sysctl -a | grep machdep.cpu | grep per_package
returns different numbers:
machdep.cpu.logical_per_package: 16
machdep.cpu.cores_per_package: 8
The desired values are:
machdep.cpu.core_count: 4
machdep.cpu.thread_count: 8
Which match:
hw.physicalcpu: 4
hw.logicalcpu: 8
CLARIFICATION
When this question was asked the OP did not say that he wanted the number of LOGICAL cores rather than the actual number of cores, so this answer logically (no pun intended) answers with a way to get the actual number of real physical cores, not the number that the OS tries to virtualize through hyperthreading voodoo.
UPDATE TO HANDLE FLAW IN YOSEMITE
Due to a weird bug in OS X Yosemite (and possibly newer versions, such as the upcoming El Capitan), I've made a small modification. (The old version still worked perfectly well if you just ignore STDERR, which is all the modification does for you.)
Every other answer given here either
bundle install --jobs 3
where you want the number in place of 3
to be one less than the number of cores you've got, or at least not more than the number of cores)The way to get just the number of cores, reliably, correctly, reasonably quickly, and without extra information or even extra characters around the answer, is this:
system_profiler SPHardwareDataType 2> /dev/null | grep 'Total Number of Cores' | cut -d: -f2 | tr -d ' '
On a MacBook Pro running Mavericks, sysctl -a | grep hw.cpu
will only return some cryptic details. Much more detailed and accessible information is revealed in the machdep.cpu
section, ie:
sysctl -a | grep machdep.cpu
In particular, for processors with HyperThreading
(HT), you'll see the total enumerated CPU count (logical_per_package
) as double that of the physical core count (cores_per_package
).
sysctl -a | grep machdep.cpu | grep per_package
This can be done in a more portable way:
$ nproc --all32
Compatible with macOS and Linux.