diff --git a/README.md b/README.md index 1427160..6c1a56e 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/primes.swift b/primes.swift new file mode 100644 index 0000000..8b594c9 --- /dev/null +++ b/primes.swift @@ -0,0 +1,42 @@ +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 { + var j: Int = (m*m - 3) / 2 + while j < half { + s[j] = 0 + j += m + } + } + 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.") +} + diff --git a/run.sh b/run.sh index aca7c8a..e33b1f4 100755 --- a/run.sh +++ b/run.sh @@ -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" ##