Skip to content

Instantly share code, notes, and snippets.

@rkh
Created June 6, 2011 09:30
Show Gist options
  • Select an option

  • Save rkh/1009994 to your computer and use it in GitHub Desktop.

Select an option

Save rkh/1009994 to your computer and use it in GitHub Desktop.

Revisions

  1. rkh created this gist Jun 6, 2011.
    13 changes: 13 additions & 0 deletions cpu_count.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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