Skip to content

List append and sum benchmark #860

Open
@certik

Description

@certik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions