Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict benchmark #1000

Open
certik opened this issue Aug 20, 2022 · 2 comments
Open

Dict benchmark #1000

certik opened this issue Aug 20, 2022 · 2 comments

Comments

@certik
Copy link
Contributor

certik commented Aug 20, 2022

See here: https://gist.github.com/czgdp1807/e7f7b6ca52c57b16b27ec8d0259c6d4a

There are multiple C++ versions, on my computer this one seems to be the fastest:

#include <unordered_map>
#include <iostream>

size_t size = 100000000;

struct ModuloHash {
    size_t operator() (int32_t key) const
    {
        return key % size;
    }
};

void test_dict() {
    std::unordered_map<int32_t, double, ModuloHash> rollnumber2cpi;
    int32_t i;
    double total = 0;
    rollnumber2cpi.reserve(size);

    for(i = 1000; i < 1000 + size; i++) {
        rollnumber2cpi[i] = double(i/100.0 + 5.0);
    }

    for(i = 1000; i < 1000 + size; i++) {
        total += rollnumber2cpi[i];
    }

    std::cout<<total<<" "<<rollnumber2cpi.size();
}

int main() {
    test_dict();
}

And here is a straightforward LPython version:

from ltypes import i32, f64

def test_dict():
    rollnumber2cpi: dict[i32, f64] = {0: 1.1}
    i: i32
    size: i32 = 100000000
    total: f64 = 0

    for i in range(1000, 1000 + size):
        rollnumber2cpi[i] = float(i/100.0 + 5.0)

    for i in range(1000, 1000 + size):
        total += rollnumber2cpi[i]

    print(total, len(rollnumber2cpi))

test_dict()

This gives me:

$ clang++ -O3 -funroll-loops -std=c++20 -ffast-math a.cpp 
$ time ./a.out
5.00015e+13 100000000./a.out  3.37s user 0.47s system 98% cpu 3.882 total
$ lpython --fast a.py
$ time ./a.out
50001499500000.00000000000000000 100000001
./a.out  0.41s user 0.25s system 96% cpu 0.684 total
@Thirumalai-Shaktivel
Copy link
Collaborator

Intel® Core™ i5-8250U CPU @ 1.60GHz × 8 (Ubuntu 22)

$ clang++ -std=c++20 -O3 -funroll-loops -march=native -ffast-math a.cpp
$ time ./a.out
5.00015e+13 100000000./a.out  5.52s user 2.00s system 99% cpu 7.527 total
$ lpython --fast a.py
$ time ./a.out
50001499500000.00000000000000000 100000001
./a.out  1.97s user 1.30s system 99% cpu 3.270 total

@Smit-create
Copy link
Collaborator

Apple Mac M1 (2020):

% clang++ -O3 -funroll-loops -std=c++20 -ffast-math a.cpp -o a.out
% time ./a.out
5.00015e+13 100000000./a.out  5.26s user 1.56s system 90% cpu 7.498 total

% lpython --fast expr.py   
% time ./a.out
50001499500000.00000000000000000 100000001
./a.out  0.97s user 0.81s system 88% cpu 2.016 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants