Speed of hash tables compared to lua
Domingo
mingodad at gmail.com
Sun Sep 13 10:20:06 UTC 2020
On Sunday, 13 September 2020 at 10:06:56 UTC, Domingo wrote:
> On Sunday, 13 September 2020 at 09:50:27 UTC, Domingo wrote:
>> Hello !
>> Doing some tests I found that D hash tables are a lot slower
>> than Lua tables see bellow, D is using 2 times more memory and
>> 5 times more time compared to Lua, when comparing with Luajit
>> it's unbelievable worse.
>>
>> I would expect a compiled language like D to use less
>> resources than a scripting language, anyone have experienced
>> something similar ?
>>
>> Does the community know this ?
>>
>> I hope this simple test would help to test and improve the
>> situation !
>>
>
> After writing the previous message I realized the test was not
> exactly fair, Lua was probably using the array functionality of
> it's table so I changed it a bit and now Lua uses as much
> memory as D but still is faster by a good margin (~4).
>
> =====
> local dsize = 6000008;
> local d = {};
>
> for i=dsize, 1, -1
> do
> if ((i % 2) == 0) then d[i] = 0.0000000123;
> else d[i] = 12345.0000000001; end
> end
>
> local sum = 0.0;
> for i=dsize, 1, -1
> do
> sum = sum + d[i];
> end
>
> print(string.format("count: %d, sum: %35.15f\n", #d, sum));
> =====
> =====
> /usr/bin/time lua sum-test-rev.lua
> count: 6000008, sum: 37035049380.000160217285156
>
> 0.63user 0.11system 0:00.74elapsed 100%CPU (0avgtext+0avgdata
> 264500maxresident)k
> 0inputs+0outputs (0major+98435minor)pagefaults 0swaps
> =====
> =====
> /usr/bin/time luajit sum-test-rev.lua
> count: 6000008, sum: 37035049380.000160217285156
>
> 0.48user 0.04system 0:00.52elapsed 100%CPU (0avgtext+0avgdata
> 117240maxresident)k
> 0inputs+0outputs (0major+41086minor)pagefaults 0swaps
>
> =====
And here is the C++ using unordered map, c++ speed is equal to
Lua but uses more memory:
=====
#include <unordered_map>
#include <string>
#include <cstdio>
int main()
{
const int DSIZE = 6000008;
// Create an empty unordered_map
std::unordered_map<int, double> intMap;
for (int i = 0; i < DSIZE; ++i)
{
intMap.insert( { i, (i % 2) == 0 ? 0.0000000123 :
12345.0000000001 });
}
double sum = 0.0;
for (std::pair<int, double> element : intMap)
sum += element.second;
printf("count: %d, sum: %35.15f\n", DSIZE, sum);
return 0;
}
=====
=====
g++ -O2 -o sum_test_aa-cpp sum_test_aa.cpp
/usr/bin/time ./sum_test_aa-cpp
count: 6000008, sum: 37035049380.000160217285156
0.49user 0.09system 0:00.59elapsed 99%CPU (0avgtext+0avgdata
330508maxresident)k
0inputs+0outputs (0major+93595minor)pagefaults 0swaps
=====
More information about the Digitalmars-d
mailing list