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

List append and sum benchmark #860

Open
certik opened this issue Aug 1, 2022 · 4 comments
Open

List append and sum benchmark #860

certik opened this issue Aug 1, 2022 · 4 comments

Comments

@certik
Copy link
Contributor

certik commented Aug 1, 2022

This benchmark is from https://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/

Python:

from ltypes import i32, i64

def test_list(n: i32) -> i64:
    a: list[i32] = [0]
    i: i32
    for i in range(n):
        a.append(i)
    s: i64 = 0
    for i in range(n):
        s += a[i]
    return s

print(test_list(100000002))

and C++:

#include <vector>
#include <iostream>

int64_t test_list(int32_t n) {
    std::vector<int32_t> a = {0};
    for (int32_t i = 0; i < n; i++) {
        a.push_back(i);
    }
    int64_t s=0;
    for (int32_t i = 0; i < n; i++) {
        s += a[i];
    }
    return s;
}

int main() {
    std::cout << test_list(100000002) << std::endl;
    return 0;
}

Results on Apple M1 Max:

$ clang++ -std=c++17 -O3 a.cpp
$ time ./a.out
5000000050000000
./a.out  0.06s user 0.07s system 91% cpu 0.145 total
$ lpython --fast a.py
$ time ./a.out
5000000050000000
./a.out  0.08s user 0.04s system 97% cpu 0.126 total
$ time PYTHONPATH=src/runtime/ltypes python a.py
5000000050000000
PYTHONPATH=src/runtime/ltypes python a.py  6.63s user 0.47s system 99% cpu 7.107 total
@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Aug 1, 2022

Results on Intel® Core™ i5-8250U CPU @ 1.60GHz × 8 (OS: Ubuntu 22.04 LTS)

$ clang++ -std=c++17 -O3 -march=native a.cpp
$ time ./a.out
5000000050000000
./a.out  0.28s user 0.25s system 99% cpu 0.532 total
$ lpython --fast a.py
$ time ./a.out
5000000050000000
./a.out  0.14s user 0.11s system 99% cpu 0.246 total
$ time python a.py
5000000050000000
python a.py  13.77s user 1.40s system 99% cpu 15.178 total

@Smit-create
Copy link
Collaborator

Apple Air Mac M1 (2020)

% lpython expr.py
% time ./a.out 
5000000050000000
./a.out  0.50s user 0.14s system 41% cpu 1.557 total

% lpython --fast expr.py
% time ./a.out          
5000000050000000
./a.out  0.13s user 0.13s system 40% cpu 0.617 total

% clang++ -std=c++17 -O3 test_cpp.cpp -o b.out
% time ./b.out
5000000050000000
./b.out  0.09s user 0.10s system 20% cpu 0.922 total

% time PYTHONPATH=$PWD/src/runtime/ltypes python expr.py
5000000050000000
PYTHONPATH=$PWD/src/runtime/ltypes python expr.py  12.13s user 2.63s system 90% cpu 16.368 total

@czgdp1807
Copy link
Collaborator

Comparison with codon,

Compiler Time [s] Relative
LPython (lpython --fast) 0.156 1.0
codon 0.15.5 (codon build -release -exe and time ./executable) 0.654 4.19

Codes

from ltypes import i32, i64

def test_list(n: i32) -> i64:
    a: list[i32] = [i32(0)]
    i: i32
    for i in range(n):
        a.append(i)
    s: i64 = i64(0)
    for i in range(n):
        s += i64(a[i])
    return s

print(test_list(100000002))
def test_list(n):
    a = [0]
    for i in range(n):
        a.append(i)
    s = 0
    for i in range(n):
        s += a[i]
    return s

print(test_list(100000002))

@certik
Copy link
Contributor Author

certik commented Aug 3, 2023

Here is the updated benchmark:

from lpython import i32, i64

def test_list(n: i32) -> i64:
    a: list[i32] = [0]
    i: i32
    for i in range(n):
        a.append(i)
    s: i64 = i64(0)
    for i in range(n):
        s += i64(a[i])
    return s

print(test_list(100000002))

It still runs at the same speed for me.

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

4 participants