Are Infs still faster than NaNs?

3 views (last 30 days)
Matt J
Matt J on 5 Jan 2014
Edited: Matt J on 8 Jan 2014
Years ago, I was advised that the processing of Inf elements was much faster than the processing of NaN elements, and was advised to use Infs instead of NaNs unless there was a compelling reason. When I replaced NaNs with Infs, I did indeed find that Infs crunched much faster. In recent days, though (R2013), I'm finding that the playing field is pretty even among Infs, NaNs, and finite array elements, e.g.,
>> A=nan(3e3); tic;fft2(A);toc
Elapsed time is 0.192480 seconds.
>> A=inf(3e3); tic;fft2(A);toc
Elapsed time is 0.200329 seconds.
>> A=rand(3e3); tic;fft2(A);toc
Elapsed time is 0.211940 seconds.
Was I hallucinating back then, or has there been an improvement in the hardware/software that handles NaNs and Infs?
  2 Comments
dpb
dpb on 5 Jan 2014
Don't recall ever hearing it; could be.
Earliest version installed here is R12; essentially same result as yours above with it (altho precedes time at which nan and inf had arguments). So, if were so, would seem to have been even earlier.
Matt J
Matt J on 5 Jan 2014
Here's the link to the original Newsgroup discussion.
I knew I didn't imagine it. And I'm definitely not the only one in that thread that witnessed slow NaNs.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 5 Jan 2014
Edited: Matt J on 5 Jan 2014
Hmmm, possibly a false alarm. In the tests below, minimization operations involving NaNs are still noticeably slower,
n=1e4;k=1e4;
A=rand(n,k);b=randi(k,n,1);
idx=bsxfun(@lt,b,(1:k));
A(idx)=nan;
tic
c=min(A,[],2);
toc
%Elapsed time is 0.348245 seconds.
A(idx)=inf;
tic
c=min(A,[],2);
toc
%Elapsed time is 0.126220 seconds.
Still, I seem to remember that a lot of other operations were slower, too.

More Answers (1)

Jan
Jan on 5 Jan 2014
It depends on the processor. AMD CPUs and SSE engines of Intel cpus process NaNs efficiently.
  1 Comment
Matt J
Matt J on 5 Jan 2014
Edited: Matt J on 5 Jan 2014
I've always used Intel CPUs. Can't imagine that accounts for what I saw, unless the efficiencies came only recently.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!