Skip to content

'too simple' benchmark #1664

Open
Open
@gptsarthak

Description

@gptsarthak

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

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