ebonnal / streamable

[Python] Stream-like manipulation of iterables.
Apache License 2.0
131 stars 0 forks source link

Measure the impact of `PyPy` on performances #10

Closed ebonnal closed 5 months ago

ebonnal commented 5 months ago

Added pypy's 30 times runtime improvement to the README

ebonnal commented 5 months ago

Few rough runtime orders of magnitude

Base snippet:

# main.py
from streamable import Stream
print(
    sum(
        Stream(range(1, 1_000_000_000))
        .map(lambda n: 1/n)
    )
)

interpreted by CPython

 % time python3 main.py
21.30048150134855
python3 main.py  102.75s user 0.09s system 99% cpu 1:43.61 total

interpreted by PyPy

% time pypy3 main.py
21.30048150134855
pypy3 main.py  2.12s user 0.02s system 99% cpu 2.162 total

Java stream

// 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

C for loop

// 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

Rust iterators

// 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