# Could I see a significant time boost using int16 instead of double on large array operations?

2 views (last 30 days)
Doug Rank on 2 Aug 2019
Answered: Walter Roberson on 2 Aug 2019
I am using the genetic algorithm with a custom fitness function that is necessarily very complicated.
Many addition/subtraction operations are performed on a large (300,000x10x10) 3D array to get the final result. All of the operations are conducted on complex integers.
This causes difficulty as the generations can take >10 minutes each with a population size of 500. The solution space is VERY large (~10^100) so it's important to get as many generations as possible during a fixed time window (a couple hours).
Would converting all of the objects to int16 or uint16 save a significant amount of time here? My guess is yes, but I don't know if Matlab does some smart things with doubles when they are all actually integers - and it would take quite a bit of work to recast all the objects.

Walter Roberson on 2 Aug 2019
No, no time would be saved, as the operation is not permitted.
>> foo = complex(randi(1000,3000,10,10,'uint16'), randi(1000,3000,10,10,'uint16'));
>> bar = complex(randi(1000,3000,10,10,'uint16'), randi(1000,3000,10,10,'uint16'));
>> for K = 1 : 20; t(K) = timeit(@() foo-bar, 0); end
Error using -
Complex integer arithmetic is not supported.
Error in @()foo-bar
Error in timeit>roughEstimate (line 182)
f();
Error in timeit (line 61)
t_rough = roughEstimate(f, num_outputs);
>> for K = 1 : 20; t(K) = timeit(@() foo+bar, 0); end
Error using +
Complex integer arithmetic is not supported.
Error in @()foo+bar
Error in timeit>roughEstimate (line 182)
f();
Error in timeit (line 61)
t_rough = roughEstimate(f, num_outputs);
However:
>> foo = complex(randi(1000,3000,10,10,'uint16'), randi(1000,3000,10,10,'uint16'));
>> bar = complex(randi(1000,3000,10,10,'uint16'), randi(1000,3000,10,10,'uint16'));
>> food = double(foo); bard = double(bar);
>> for K = 1 : 20; t(K) = timeit(@() complex(real(foo)-real(bar),imag(foo)-imag(bar)), 0); end
>> for K = 1 : 20; td(K) = timeit(@() food-bard, 0); end
>> mean(td)/mean(t)
ans =
3.37810543407242
so there are gains to be made if you are willing to do all of the real() and imag() yourself, which should be valid for addition and subtraction.