Slow code: is it iptcheckinput?
显示 更早的评论
Hi everyone,
Currently im creating some software to do image segmentation. The software I've created does some morphologic operations and then uses BWLabel and RegionProps to select the largest element from the image.
First of all, the code itself is quite fast I'd say (like 0.04 secs per image), but I have to run it like thousands of times (I want to tweak some parameters using a genetic algorithm).
I've already tried to optimize my code the best, and I've been able to speed it up some more. But now when I was profiling it, I noticed the following.
Regionprops uses the function iptcheckinput to validate it's input? Is this really neccicarily? Because that function alone takes about 50% of the execution time right now. And is there a way I can optimize it?
Thanks in advance,
Timo Willemsen
回答(1 个)
Jan
2011-12-22
0 个投票
The function iptcheckinput is a fast C++-Mex function, which e.g. checks if all values elementes of the inputs are positive integers. This can consume a lot of time and if all inputs are valid, this time is wasted. Therefore I suggest to copy the original functions and use a flag to disable the iptcheckinput in standard mode -- but allow a debug-mode. E.g. this can be triggered by an input argument or by moving the original code in a TRY-CATCH block and running the code with all checks in case of errors automatically.
Remember: Unneeded checks waste seconds of runtime in case of valid inputs. Omitted checks waste hours and days for frustrating debugging in case of invalid input. So thinking twice is recommended and caring for a secure fallback is obligatory. If your function is used by others an exhaustive documentation is required.
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!