问题描述
当您运行Mac OS X时,如何从命令行得知计算机上有多少个内核?在Linux上,我使用:
How can you tell, from the command line, how many cores are on the machine when you're running Mac OS X? On Linux, I use:
x=$(awk '/^processor/ {++n} END {print n+1}' /proc/cpuinfo)
这不是完美的,但是很接近.打算将其馈送到make
,这就是为什么它得出的结果比实际数字高1的原因.而且我知道上面的代码可以用Perl编写得更密集,也可以使用grep,wc和cut来编写,但是我认为上面的代码在简洁性和可读性之间取得了很好的权衡.
It's not perfect, but it's close. This is intended to get fed to make
, which is why it gives a result 1 higher than the actual number. And I know the above code can be written denser in Perl or can be written using grep, wc, and cut, but I decided the above was a good tradeoff between conciseness and readability.
非常晚的为了澄清一下:我要问有多少逻辑内核可用,因为这与我想要make
进行的同时作业数量相对应产生.克里斯·劳埃德(Chris Lloyd)进一步完善了jkp的答案,这正是我所需要的. YMMV.
VERY LATE Just to clarify: I'm asking how many logical cores are available, because this corresponds with how many simultaneous jobs I want make
to spawn. jkp's answer, further refined by Chris Lloyd, was exactly what I needed. YMMV.
推荐答案
您可以使用 sysctl 实用程序:
sysctl -n hw.ncpu
这篇关于如何在Mac OS X上发现*逻辑*内核的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!