Speed of masked matrix operations in 'single' vs 'double'
Show older comments
I've been going through a lot of my tools and trying to make things faster and reduce memory use. I know that using double-precision FP as my default working datatype is part of the problem regarding memory, but I had expected using single-precision may be faster as well.
Simple tests seem to indicate that it would be (these times are all averages of many tests):
% 105.6ms for double
% 50.4ms for single
R=imfilter(bg,fs);
% 7.6ms for double
% 4.4ms for single
R=flipdim(bg,2);
% 45ms double
% 22ms single
R=bg.*fg;
% 4.5ms double
% 3.2ms single
R=fg.^2 + 2*bg.*fg.*(1-bg);
but operations involving masking via multiplication were significantly slower in single:
% 6.0ms double
% 25.7ms single!
hi=I>0.5;
R=(1-2*(1-I).*(1-M)).*hi + (2*M.*I).*~hi;
Explicitly casting the logical mask as numeric and handling it without the NOT operator does speed things up a bit, but either case with numeric masks is still slower than using double with logical masks.
% 7.7ms double
% 9.8ms single
hi=single(I>0.5);
R=(1-2*(1-I).*(1-M)).*hi + (2*M.*I).*(1-hi);
You might ask why I'm masking via multiplication in the first place. Why not just use logical indexing? I used to do everything that way, but apparently overcalculation is faster than a bunch of logical indexing:
% 62.1ms double
% 53.7ms single
hi=I>0.5; lo=~hi;
R=zeros(size(I),'single'); % preallocate with appropriate wclass
R(lo)=2*I(lo).*M(lo);
R(hi)=1-2*(1-M(hi)).*(1-I(hi));
Am I misguided to expect reliable speed gains from using single-precision for a wide range of operations across different machines (this code will be used by others)? Comments like this make me think so.
That, and if I were to pursue this flexibility for the conservation of memory alone, is there a better approach to masked operations than what I've described?
Accepted Answer
More Answers (1)
Image Analyst
on 11 Jul 2018
0 votes
According to Intel (10 years ago so might not be true still today), double should be slower than single :
but there are lots of "depends" and ways to speed it up. See the discussion for details.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!