Closed ebonnal closed 5 months ago
Base snippet:
# main.py
from streamable import Stream
print(
sum(
Stream(range(1, 1_000_000_000))
.map(lambda n: 1/n)
)
)
% time python3 main.py
21.30048150134855
python3 main.py 102.75s user 0.09s system 99% cpu 1:43.61 total
% time pypy3 main.py
21.30048150134855
pypy3 main.py 2.12s user 0.02s system 99% cpu 2.162 total
// Main.java
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
double sum = IntStream
.range(1, 1_000_000_000)
.mapToDouble(n -> 1.0 / n)
.sum();
System.out.println(sum);
}
}
% javac Main.java
% time java -XX:+UseG1GC Main
21.300481501347942
java -XX:+UseG1GC Main 4.15s user 0.03s system 99% cpu 4.200 total
// main.c
#include <stdio.h>
int main() {
double sum = 0.0;
for (long long int i = 1; i < 1000000000; ++i) {
sum += 1.0 / i;
}
printf("%.15f\n", sum);
return 0;
}
% gcc -O3 -o main_optimized.out main.c
% time ./main_optimized.out
21.300481501348550
./main_optimized.out 1.02s user 0.00s system 99% cpu 1.034 total
// main.rs
fn main() {
let sum: f64 = (1..1_000_000_000)
.map(|n| 1.0 / n as f64)
.sum();
println!("{}", sum);
}
% rustc -C opt-level=2 ./main.rs
% time ./main
21.30048150134855
./main 1.00s user 0.00s system 98% cpu 1.014 total
Added
pypy
's 30 times runtime improvement to the README