Merge pull request #1 from boromil/master

add: Go and PyPy
master
Ivan Zahariev 2016-07-26 14:21:51 +03:00 committed by GitHub
commit f054d5ab6e
3 changed files with 59 additions and 1 deletions

View File

@ -1,5 +1,5 @@
# langs-performance
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS performance benchmark
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS vs. Go performance benchmark
Blog articles:
* 2016: http://blog.famzah.net/2016/02/09/cpp-vs-python-vs-perl-vs-php-performance-benchmark-2016/

55
primes.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"fmt"
"math"
)
func getPrimes7(n int) []int {
if n < 2 {
return []int{}
}
if n == 2 {
return []int{2}
}
s := []int{}
for i := 3; i <= n; i += 2 {
s = append(s, i)
}
var j int
m := 3
mroot := int(math.Sqrt(float64(n)))
half := len(s)
for i := 0; m <= mroot; {
if s[i] != 0 {
j = (m*m - 3) / 2
s[j] = 0
for j < half {
s[j] = 0
j += m
}
}
i++
m = 2*i + 3
}
res := []int{}
res = append(res, 2)
for _, v := range s {
if v != 0 {
res = append(res, v)
}
}
return res
}
func main() {
var res []int
for i := 0; i < 10; i++ {
res = getPrimes7(10000000)
fmt.Printf("Found %d prime numbers.\n", len(res))
}
}

3
run.sh
View File

@ -33,6 +33,9 @@ C='g++' ; run_benchmark 'C++ (optimized with -O2)' "$C -Wall -O2 primes.cpp -o
rm -f ./primes.cpp.out
C='g++' ; run_benchmark 'C++ (not optimized)' "$C -Wall primes.cpp -o primes.cpp.out" './primes.cpp.out' "$C --version" 'head -n1'
rm -f ./primes.cpp.out
C='go' ; run_benchmark 'Go (not optimized, default compiler)' "$C build primes.go" './primes' "$C version" 'cat'
go clean
C='pypy' ; run_benchmark 'PyPy 2.7' 'true' "$C ./primes.py" "$C -V" 'cat'
C='python2.7' ; run_benchmark 'Python 2.7' 'true' "$C ./primes.py" "$C -V" 'cat'
C='python3.2' ; run_benchmark 'Python 3.2' 'true' "$C ./primes.py" "$C -V" 'cat'
C='python3.5' ; run_benchmark 'Python 3.5' 'true' "$C ./primes.py" "$C -V" 'cat'