Skip to content

Latest commit

 

History

History
106 lines (79 loc) · 2.17 KB

File metadata and controls

106 lines (79 loc) · 2.17 KB

Benchmark: Sum of 0 to 100 Million

This benchmark measures a simple loop summing integers from 0 to 100,000,000.

Results

Language Optimization Execution Time vs Python
Python - 6.556s 1x
DynLex O0 0.196s 33x faster
C++ O0 0.186s 35x faster
DynLex O3 0.001s 6556x faster
C++ O3 0.001s 6556x faster

All outputs: 4999999950000000 (correct)

Compilation Times

Compiler Time
DynLex ~50ms
g++ ~35ms

Notes

With -O3 optimization, both DynLex and C++ compute the result at compile time via LLVM constant folding, making execution essentially instant.

Without optimization, DynLex is ~5% slower than C++ due to function call overhead for the set variable to value pattern (not yet inlined without optimization).

Source Code

DynLex (benchmark.dl)

flex function return value:
    replacement:
        @intrinsic("return", value)

flex function set variable to value:
    replacement:
        @intrinsic("store", variable, value)

function print message as a line:
    replacement:
        @intrinsic("discard", @intrinsic("variadic call", "libc", "printf", an integer, 1, "%ld\n", message))

function left < right:
    execute:
        return @intrinsic("less than", left, right)

flex function left + right:
    replacement:
        @intrinsic("add", left, right)

flex section loop while condition:
    replacement:
        @intrinsic("loop while", condition)

set sum to 0
set index to 0
loop while index < 100000000:
    set sum to sum + index
    set index to index + 1
print sum as a line

C++ (benchmark.cpp)

#include <cstdio>

int main() {
    long sum = 0;
    for (long i = 0; i < 100000000; i++) {
        sum = sum + i;
    }
    printf("%ld\n", sum);
    return 0;
}

Python (benchmark.py)

sum = 0
i = 0
while i < 100000000:
    sum = sum + i
    i = i + 1
print(sum)

How to Run

# DynLex
./build/dynlex benchmark.dl -O3 -o bench_dynlex
time ./bench_dynlex

# C++
g++ -O3 benchmark.cpp -o bench_cpp
time ./bench_cpp

# Python
time python3 benchmark.py