Codes
from ltypes import i32, f64
def test_dict():
rollnumber2cpi: dict[i32, f64] = {0: 1.1}
i: i32
size: i32 = 100000000
total: f64 = 0
for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
for i in range(1000, 1000 + size):
total += rollnumber2cpi[i]
print(total, len(rollnumber2cpi))
test_dict()
#include <unordered_map>
#include <iostream>
void test_dict() {
std::unordered_map<int32_t, double> rollnumber2cpi;
int32_t i, size = 100000000;
double total = 0;
for(i = 1000; i < 1000 + size; i++) {
rollnumber2cpi[i] = double(i/100.0 + 5.0);
}
for(i = 1000; i < 1000 + size; i++) {
total += rollnumber2cpi[i];
}
std::cout<<total;
}
int main() {
test_dict();
}
#include <unordered_map>
#include <iostream>
size_t size = 100000000;
struct ModuloHash {
size_t operator() (int32_t key) const
{
return key % size;
}
};
void test_dict() {
std::unordered_map<int32_t, double, ModuloHash> rollnumber2cpi;
int32_t i;
double total = 0;
rollnumber2cpi.reserve(size);
for(i = 1000; i < 1000 + size; i++) {
rollnumber2cpi[i] = double(i/100.0 + 5.0);
}
for(i = 1000; i < 1000 + size; i++) {
total += rollnumber2cpi[i];
}
std::cout<<total<<" "<<rollnumber2cpi.size();
}
int main() {
test_dict();
}
codon
code
def test_dict():
rollnumber2cpi = {0: 1.1}
size = 100000000
total = 0.0
for i in range(1000, 1000 + size):
rollnumber2cpi[i] = float(i/100.0 + 5.0)
for i in range(1000, 1000 + size):
total += rollnumber2cpi[i]
print(total, len(rollnumber2cpi))
test_dict()
Apple M1 Macbook Pro macOS Monterey 12.5
Compiler | Time [s] | Relative |
---|---|---|
LPython (dict02) | 2.502 | 2.00 |
LPython (dict03) | 1.87 | 1.5 |
LPython (dict03) OptimizedLinearProbing | 1.85 | 1.48 |
LPython SeparateChaining | 4.931 | 4.0 |
LPython (dict_neg_keys) --fast | 0.984 | 0.8 |
LPython (dict_neg_keys) | 1.922 | 1.5 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 -std=c++11 | 30.814 | 24.73 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 | 38.581 | 30.96 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 ModuloHash | 36.846 | 29.457 |
LPython (dict02) --fast | 1.246 | 1.0 |
LPython (dict03) --fast | 1.1 | 0.88 |
LPython (dict03) --fast OptimizedLinearProbing | 1.104 | 0.88 |
LPython --fast SeparateChaining | 4.270 | 3.4 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 -O3 -funroll-loops -std=c++11 | 7.386 | 5.93 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 -O3 -funroll-loops | 7.573 | 6.08 |
clang++ 13.1.6 arm64-apple-darwin21.6.0 -O3 -funroll-loops -ffast-math ModuloHash | 7.814 | 6.27 |
Python 3.10.4 | 22.678 | 18.20 |
codon 0.15.5 (codon build -release -exe and time ./executable ) |
4.983 | 4.0 |
Both the computation time (user) and system calls (sys) are lower for LPython.
(lp) 12:52:38:~/lpython_project/lpython % clang++ /Users/czgdp1807/lpython_project/dict_bench_hash.cpp
(lp) 12:52:44:~/lpython_project/lpython % time ./a.out
5.00015e+13 100000000./a.out 34.17s user 1.90s system 96% cpu 37.385 total
(lp) 12:53:24:~/lpython_project/lpython % clang++ -O3 -funroll-loops -ffast-math /Users/czgdp1807/lpython_project/dict_bench_hash.cpp
(lp) 12:54:03:~/lpython_project/lpython % time ./a.out
5.00015e+13 100000000./a.out 5.47s user 1.73s system 87% cpu 8.260 total
(lp) 12:54:14:~/lpython_project/lpython % lpython /Users/czgdp1807/lpython_project/dict_bench.py
(lp) 12:54:25:~/lpython_project/lpython % time ./a.out
50001499500000.00000000000000000 100000001
./a.out 1.35s user 0.33s system 99% cpu 1.691 total
(lp) 12:54:30:~/lpython_project/lpython % lpython --fast /Users/czgdp1807/lpython_project/dict_bench.py
(lp) 12:54:49:~/lpython_project/lpython % time ./a.out
50001499500000.00000000000000000 100000001
./a.out 0.49s user 0.33s system 94% cpu 0.866 total
(lp) 12:54:52:~/lpython_project/lpython %
Ubuntu 18.04.6 on Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Compiler | Time [s] | Relative |
---|---|---|
LPython (dict02) | 2.801 | 1.23 |
LPython SeparateChaining | 4.738 | 2.1 |
clang++ version 6.0.0-1ubuntu2 -std=c++11 | 34.041 | 15.03 |
clang++ version 6.0.0-1ubuntu2 | 34.189 | 15.09 |
clang++ version 6.0.0-1ubuntu2 ModuloHash | 28.280 | 12.485 |
g++ 7.5.0 | 39.505 | 17.44 |
LPython (dict02) --fast | 2.265 | 1.0 |
LPython --fast SeparateChaining | 4.162 | 1.8 |
clang++ version 6.0.0-1ubuntu2 -O3 -march=native -funroll-loops -std=c++11 | 6.482 | 2.86 |
clang++ version 6.0.0-1ubuntu2 -O3 -march=native -funroll-loops | 6.47 | 2.85 |
clang++ version 6.0.0-1ubuntu2 -O3 -march=native -funroll-loops -ffast-math ModuloHash | 6.402 | 2.82 |
g++ 7.5.0 -O3 -march=native -funroll-loops | 8.427 | 3.72 |
Python 3.10.5 | 15.454 (Killed In Between by the OS) | 6.82 |
On my M1 Max I get:
and
and
I also tried
std::map
, then I got 14.2s.The following code:
Runs at 3.915s.
The ModuloHash above gives me: