Skip to content

How to count the CPU cores each languages on Linux

Notifications You must be signed in to change notification settings

kubo39/cpucount

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpucount

  • My environment.
$ uname -smrv
Linux 4.4.0-72-generic #93-Ubuntu SMP Fri Mar 31 14:07:41 UTC 2017 x86_64
$ cat /proc/cpuinfo| grep ^processor| wc -l
4

nproc(1)

$ nproc --version| head -1
nproc (GNU coreutils) 8.25
$ nproc
4
$ taskset -c 0,1 nproc
2

C++14

$ g++ --version | head -1
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
$ g++ -std=c++14 cpp/cpucount.cpp
$ ./a.out
4
$ taskset -c 0,1 ./a.out
4

Python

$ python --version
Python 3.6.1
$ python python/cpucount.py
4
4
$ taskset -c 0,1 python python/cpucount.py
4
2

Ruby

$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
$ ruby ruby/cpucount.rb
4
$ taskset -c 0,1 ruby ruby/cpucount.rb
2

Go

  • https://golang.org/pkg/runtime/#NumCPU

    NumCPU returns the number of logical CPUs usable by the current process. The set of available CPUs is checked by querying the operating system at process startup. Changes to operating system CPU allocation after process startup are not reflected.

$ go version
go version go1.6.2 linux/amd64
$ go run golang/cpucount.go
4
$ taskset -c 0,1 go run golang/cpucount.go
2

Rust

$ rustc --version  # num_cpus = 1.5.0
rustc 1.19.0-nightly (5b13bff52 2017-05-23)
$ cargo run -q
4
$ taskset -c 0,1 cargo run -q
2

D

$ dmd --version
DMD64 D Compiler v2.074.0
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
$ rdmd dlang/cpucount.d
4
$ taskset -c 0,1 rdmd dlang/cpucount.d
4

Nim

$ nim --help| head -1
Nim Compiler Version 0.17.0 (2017-05-17) [Linux: amd64]
$ nim c -r nim/cpucount.nim
...
4
$ taskset -c 0,1 nim c -r nim/cpucount.nim
...
4

About

How to count the CPU cores each languages on Linux

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published