Add Swift

master
Jonas Schulze 2017-12-22 16:05:11 +01:00
parent d69632e87f
commit fb6f992fd8
3 changed files with 52 additions and 1 deletions

View File

@ -1,5 +1,5 @@
# langs-performance
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS vs. Go vs. Ruby performance benchmark
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS vs. Go vs. Ruby vs. Rust vs. Swift performance benchmark
Blog articles:
* 2016: https://blog.famzah.net/2016/09/10/cpp-vs-python-vs-php-vs-java-vs-others-performance-benchmark-2016-q3/

40
primes.swift Normal file
View File

@ -0,0 +1,40 @@
import Foundation
func get_primes7(_ n: Int) -> [Int] {
if n < 2 {
return []
} else if n == 2 {
return [2]
}
// do only odd numbers starting at 3
var s = Array(stride(from: 3, to: n, by: 2))
let mroot: Int = Int(sqrt(Double(n)))
let half = s.count
var i = 0
var m = 3
while m <= mroot {
if s[i] != 0 {
for j in stride(from: (m*m - 3) / 2, to: half, by: m) {
s[j] = 0
}
}
i += 1
m += 2
}
return [2] + s.filter { $0 != 0 }
}
if let period_time_var = ProcessInfo.processInfo.environment["RUN_TIME"] {
let start_time = Date()
let period_time = Int(period_time_var)!
while Int(Date().timeIntervalSince(start_time)) < period_time {
let res = get_primes7(10_000_000)
print("Found \(res.count) prime numbers.")
}
} else {
print("RUN_TIME not found.")
}

11
run.sh
View File

@ -110,10 +110,21 @@ go clean
##
C='swiftc' ; SRC='primes.swift' ; run_benchmark 'Swift (optimized with -O)' \
"$C $SRC -o primes.swift.out -O -swift-version 4" './primes.swift.out' "$C -version" 'head -n1' "$SRC"
rm -f ./primes.swift.out
C='swiftc' ; SRC='primes.swift' ; run_benchmark 'Swift (not optimized)' \
"$C $SRC -o primes.swift.out -swift-version 4" './primes.swift.out' "$C -version" 'head -n1' "$SRC"
rm -f ./primes.swift.out
##
C='pypy' ; SRC='primes.py' ; run_benchmark 'Python 2.7 + PyPy' 'true' "$C $SRC" "$C -V" 'cat' "$SRC"
C='python2.7' ; SRC='primes.py' ; run_benchmark 'Python 2.7' 'true' "$C $SRC" "$C -V" 'cat' "$SRC"
C='python3.2' ; SRC='primes.py' ; run_benchmark 'Python 3.2' 'true' "$C $SRC" "$C -V" 'cat' "$SRC"
C='python3.5' ; SRC='primes.py' ; run_benchmark 'Python 3.5' 'true' "$C $SRC" "$C -V" 'cat' "$SRC"
C='python3.6' ; SRC='primes.py' ; run_benchmark 'Python 3.6' 'true' "$C $SRC" "$C -V" 'cat' "$SRC"
##