c++ - Store float as short int. Confusing results -


i'm trying save memory , time consumption storing 32bit float values in 16bit short variables. in order introduce conversion functions this:

typedef short int storetype; storetype floattoshort( float x ) { return x/k; } float shorttofloat( storetype x ) { return float(x)*k; } 

then convert input data storetype array using first function. , each time need value extract storetype data , using second function float value.

but when want estimate speed optioned because of memory optimization , compare 2 cases:

  1. is described above - when storetype == short int
  2. is same, storetype == float ( first line replaced "typedef short int storetype" ).

i see in second case time smaller (like 10% when measurement error less 1%) in spite of anticipations (calculations same, cash usage bigger float). meanwhile algorithm speed theoretically should not depend on intimidate result of calculation (i.e. precision). , final result of program practically doesn't change (less 0.2% difference) storetype changes. program includes number of computations converted values, type convertion time expected negligible in comparison computations.

how can possible program faster floats?

in first case performing floating point operations along conversion float short int (or vice versa).

in second case, have same floating point operation there no conversion. unless misunderstand question, why faster.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -