Created
March 15, 2020 13:37
-
-
Save 911992/713a84b4fb56da2a69580bea18ddc8ee to your computer and use it in GitHub Desktop.
Using one loop(for ,e.g. 1 X 16) instead of two(for-for, e.g. 4X4) for performance stuffs
This file contains 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
/** | |
#compile: | |
gcc -S -fverbose-asm -g -O0 <<FILE>>.c -O <<FILE>>.s | |
#asm | |
as -alhnd <<FILE>>.s > <<FILE>>.lst | |
#related course | |
https://www.youtube.com/watch?v=ulJm7_aTiQM | |
*/ | |
/*using one loop instead of two (nested loop)*/ | |
void loop_2d(void){ | |
/*tmp var to hold the res*/ | |
int v=0; | |
/*const the first loop iteration len*/ | |
const int l=4; | |
/*index for triggering event*/ | |
int i=0; | |
/*for a lxl (16) loop*/ | |
for(int a=0; | |
a<(l*l); | |
a++){ | |
/*if index of first loop is equal to index of second loop*/ | |
if(i==a){ | |
/*update the res*/ | |
v = v + a; | |
/*update the trigger index*/ | |
i = i + l | |
+ 1; | |
} | |
} | |
} | |
/*using nested loop*/ | |
void loop_2d2d(void) { | |
/*tmp var to hold the res*/ | |
int v=0; | |
/*const the first loop iteration len*/ | |
const int l=4; | |
/*for a li(4) loop*/ | |
for(int a=0; | |
a<l; | |
a++){ | |
/*for another li(4) loop(performance hit?)*/ | |
for(int b=0; | |
b<l; | |
b++){ | |
/*if index of first loop is equal to index of second loop*/ | |
if(b==a){ | |
/*update the trigger index*/ | |
v = v + a; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment