Skip to content

'too simple' benchmark #1664

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

Open
gptsarthak opened this issue Apr 3, 2023 · 1 comment
Open

'too simple' benchmark #1664

gptsarthak opened this issue Apr 3, 2023 · 1 comment

Comments

@gptsarthak
Copy link
Contributor

Here is the "too simple" benchmark from benchmarksgame :
https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/toosimple.html
In Python:

from lpython import i32, f64

def main():
    n : i32 = 100000000
    sum : f64 = 0.0
    flip : f64 = -1.0
    i : i32 = 0
    for i in range(1,n):    
        flip *= -1.0        
        sum += flip / (2*i - 1)                                      
    print((sum*4.0))
main() 

In C :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long n = 100000000;
    double sum = 0.0;
    double flip = -1.0;
    for (long i = 1; i <= n; i++) {    
        flip *= -1.0;        
        sum += flip / (2*i - 1);               
    }                        
    printf("%.9f\n", sum*4.0);
    return 0;      
}

Results on PopOS 22.04 on AMD 5800u CPU

% gcc -O3 -march=native  simple.c       
% time ./a.out
3.141592644
./a.out  0.18s user 0.00s system 99% cpu 0.179 total
% lpython simple.py            
3.14159266358932587e+00
% time ./simple.out
3.14159266358932587e+00
./simple.out  0.48s user 0.00s system 99% cpu 0.479 total
% lpython --fast simple.py
3.14159266358932587e+00
% time ./simple.out       
3.14159266358932587e+00
./simple.out  0.18s user 0.00s system 99% cpu 0.179 total
% time PYTHONPATH=.. python simple.py
3.141592663589326
PYTHONPATH=.. python simple.py  10.78s user 0.00s system 99% cpu 10.787 total
Compiler Time Relative
gcc 0.179 1.00
Lpython 0.479 2.67
Lpython --fast 0.179 1.00
Python 10.78 60.22

Versions:

$ gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ lpython --version
LPython version: 0.12.0-185-g609279265
Platform: Linux
Default target: x86_64-unknown-linux-gnu
$ python --version
Python 3.10.8

Result: Lpython is 60x faster than Cpython 🚀 and has the same performance as gcc

@certik
Copy link
Contributor

certik commented Apr 3, 2023

Thanks @gptsarthak for testing it! Yes, LPython must have the same or better performance than C or C++, otherwise it's a bug that we need to fix.

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

2 participants