Skip to content

Instantly share code, notes, and snippets.

@digitalextremist
Forked from rkh/cpu_count.rb
Last active August 29, 2015 14:25
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 digitalextremist/28f2c3c55f9378ae2450 to your computer and use it in GitHub Desktop.
Save digitalextremist/28f2c3c55f9378ae2450 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