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

Answers (1)

Jan
Jan on 22 Dec 2011
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.

Categories

Asked:

on 22 Dec 2011

Community Treasure Hunt

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

Start Hunting!