I've been looking at array access and noticed that C++ seems to yield code that
is 20x-30x faster for array access.
## compiled with: nim -d:r c filename
when isMainModule:
const N = 20_000_000;
var data {.noinit.}: array[N,int]
# custom init
for i in 0 ..< N:
data[i] = i
# busy work
for r in 1 .. 50:
for i in 3 ..< N-1:
data[i] = (data[i+1]-data[i])+data[i-1]
echo "result: ",data[N-2]
Run
// compiled with: c++ -O3 -o filename filename.cpp
#include <iostream>
int main()
{
const int N = 20000000;
long long data[N];
// custom init
for (long long i=0; i<N; i++) {
data[i] = i;
}
// busy work
for (int r=0; r<50; r++) {
for (long long i=3; i<N-1; i++)
{
data[i] = (data[i+1]-data[i])+data[i-1];
}
}
std::cout << "result: " << data[N-2] << std::endl;
return ( 0 );
}
Run
I checked the c++ with the godbolt service to make sure the code wasn't being
optimized away for some weird reason.