bsxfun vs implicit expansion
조회 수: 11 (최근 30일)
이전 댓글 표시
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1475561/image.png)
댓글 수: 7
Walter Roberson
2023년 9월 8일
Historically, some of the frequent volunteers have found that bsxfun was faster for some situations but not all; implicit expansion is faster for some operations.
As far as I know, there is no guideance from Mathworks as to when each of the two would be expected to be faster.
Joss Knight
2023년 9월 8일
GPU is faster because consecutive operations can be fused in the implicit case. Not sure about the CPU case. I'll ask.
답변 (1개)
Gagan Agarwal
2023년 9월 20일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!