Skip to content

Instantly share code, notes, and snippets.

@ChrisMacNaughton
Forked from rkh/cpu_count.rb
Created August 23, 2013 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMacNaughton/6321586 to your computer and use it in GitHub Desktop.
Save ChrisMacNaughton/6321586 to your computer and use it in GitHub Desktop.
module System
extend self
def cpu_count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist? '/proc/cpuinfo'
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end
end
System.cpu_count # => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment