Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

macos - How to discover number of *logical* cores on Mac OS X?

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)

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.

VERY LATE EDIT: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this using the sysctl utility:

sysctl -n hw.ncpu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...