c++ - Conditional large flat array traversal and surprisingly short loop execution time -
i in need of explanation have discovered experience. have large flat array of type char. array in total 500x500x500 = 125e+6 bytes long. inside cells keep number between 0 , 255. luckily when traversing array interested in cells having non-zero value!
now here goes question. found out experimenting doing smallest operation on cells takes huge amount of time when going through whole 0 , non-zero array whereas, if use condition similar 1 below,
while( index < 125000000 ) { if( array[ index ] > 0 ) { // stuff } index++; }
the execution time drastically shorter. in fact can go through whole array , perform operations on non-zero cells in few seconds rather half hour execution time of approach no conditions.
what need explanation of why works! need explain phenomena in thesis report , best if can relate scientific paper or similar.
thank in advance!
best regards, omid ariyan
it expect char
unsigned, hence capable of holding values in range [0,255], in fact signed, holding values in range [-128, 127] (assuming two's complement). number of cases array[ index ] > 0
smaller expect, because elements assigned values larger 127
have negative value.
note claim check non-zero values, checking positive ones.
you can check range of char
on platform:
#include <limits> #include <iostream> int main() { std::cout << static_cast<int>(std::numeric_limits<char>::min()) << std::endl; std::cout << static_cast<int>(std::numeric_limits<char>::max()) << std::endl; char c = 234; std::cout << static_cast<int>(c) << std::endl; // 234 if unsigned, -22 if signed }
Comments
Post a Comment