Created
June 26, 2018 02:23
-
-
Save melvyniandrag/671be0e9daf6da626ce55e16cf94b6db to your computer and use it in GitHub Desktop.
I get about 3* faster with the simpleStride. Seems like a compiler optimization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned long time; | |
const unsigned long N = 999; | |
const unsigned long N_sq = N * N; | |
unsigned long bigArr[N] = {0}; | |
unsigned long sum; | |
unsigned long Cache = 128; | |
const unsigned long NumIter = 10000; | |
void init_arr() { | |
for ( unsigned long i = 0; i < N; ++i) { | |
bigArr[i] = i; | |
} | |
} | |
unsigned long simpleStride( unsigned long iter ) { | |
const unsigned long ret = ( 1000 * 1000 ) % N; | |
iter = iter + ret - ret; | |
return iter; | |
} | |
unsigned long cacheStride( unsigned long iter ) { | |
unsigned long ret = ( iter * Cache ) % N; | |
ret = ret + ret - ret; | |
return ret; | |
} | |
void timerFunction( unsigned long (*f)(unsigned long) , const char* strideFunName) { | |
sum = 0; | |
time = millis(); | |
for ( unsigned long iter = 0; iter < NumIter; ++iter ) { | |
for ( unsigned long i = 0; i < N; ++i ) { | |
sum += bigArr[f(i)]; | |
} | |
} | |
time = millis() - time; | |
Serial.print( strideFunName ); | |
Serial.print( " : " ); | |
Serial.print( time ); | |
Serial.print( " sum: " ); | |
Serial.print( sum ); | |
Serial.print( "\n" ); | |
} | |
void setup() { | |
Serial.begin( 9600 ); | |
init_arr(); | |
} | |
void loop() { | |
timerFunction( simpleStride, "stride of 1 " ); | |
timerFunction( cacheStride, "stride of 128" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment