bsxfun vs implicit expansion
8 views (last 30 days)
Show older comments
The MATLAB docs recommend that calls to bsxfun are replaced with direct calls to the functions and operators that support implicit expansion. I have seen this same recommendation in several places on the file exchange (etc). However, a simple benchmark (see attached) seems to show that bsxfun is noticably faster, at least for this particular use case I'm interested in (complex vector times a complex 2D or 3D matrix with relatively large dimensions). The output image below is running in MATLAB 2023a. I get the same trend under both Windows and Linux on both new and old hardware. Is this an expected result? I couldn't find many details of what actually happens under the hood for both of these approaches and when it might be better to use one over the other.
9 Comments
DGM
on 17 Oct 2024
Sometimes the ideal choice is easy to identify. I'd prefer using implicit expansion all day, but anything I write for my toolbox is intended to work in legacy versions, so I use bsxfun() everywhere instead. At least in R2019b, the speed benefit of either choice never seemed to be consistently significant.
... I suppose then the question becomes whether it's worthwhile to conditionally switch between different implementations depending on version. Is the occasional time advantage worth the cost of version checking, ugliness, and hassle?
Bruno Luong
on 18 Oct 2024
This is the result of R2024b on ly Laptop (Intel Ultra 9 185H). Bsxfun is still has an edge in speed
Answers (2)
Gagan Agarwal
on 20 Sep 2023
Hi Bradley,
I understand that you want to know the scenarios for which ‘bxfun’ is preferred and the scenarios for which use of ‘implicit expansion ‘is preferred.
In general, it has been observed that ‘implicit expansion’ tends to be faster than ‘bsxfun’. This is especially true when working with GPUs, where ‘implicit expansion’ often outperforms ‘bsxfun’. However, I couldn't find any specific scenarios that definitely determine when to use one over the other. Therefore, the best approach would be to decide on a case-to-case basis.
I hope this helps!
0 Comments
Bobby Cheng
on 17 Oct 2024
Hi Bradley,
Just come across this today. To answer you question, you should always use Implicit Expansion instead of bsxfun whenever possible. We expect Implicit expansion to be faster or run at similar time. Everytime you see a performance regression from switching from bsxfun, please consider this as a bug and contact customer support .
Using your benchmark, I was able to find a few performance edge cases and have reported them to the development.
More importantly, using implicit expansion, you will have more readable code. And if you have several calls to bsxfun, like
tmp = bsxfun(@times, x, y);
z = bsxfun(@times, tmp, w);
then you are likely to write it as
z = x .* y + w;
This is what we observe in practice. MATLAB execution engine has taken this into consideration and would execute without the need to create temporary array, and explore the coarse grain parallelism by chaining the operations.
Because of this more complex setting, there is some subtle differences in parallelization strategy compared to the relatively simple case in bsxfun. Implicitly expansion is a lot better in more complex expression.
For your simple test cases, bsxfun can do really well in term of performance and is competitive with implicit expansion. But if you look at your code closely, you want to write ifft( (1i*kx) .* fft(f)). That is, you could have written the benchmark as 1i .* kx .* mat3D instead of forming 1i.*kx first and create an unnecessary temporary array.
I have not tested the performance difference of the two approaches, but I think this shows how easy to write a more complex expression using implicit expansion then using bsxfun.
Hope this helps,
---Bob.
0 Comments
See Also
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!