in reply to Re: Double the speed of Imager->setpixel
in thread Double the speed of Imager->setpixel

That is also an option, but testing for a flag itself takes time (and adds a degree of complexity).

My preference for this sort of thing is to have an internal method (eg _setpixel) that gets called to do the real work once all sanity checks have been performed, and where appropriate to expose that in documentation with suitable warnings.

Quite often one method invokes other methods in the same class with parameters it has already checked for sanity, so the availability of such internal methods can also give a free speed boost even when external users do not invoke them directly.

Replies are listed 'Best First'.
Re^3: Double the speed of Imager->setpixel
by swl (Prior) on Dec 12, 2022 at 05:41 UTC

    True.

    Looking at the setpixel method it should also be possible to write a variant that iterates over a list of values, e.g. [x1,y1,colour1, x2,y2,colour2, ...] or arrays of x, y and colour. Then the validity checks are called once per method invocation regardless of the number of colour assignments. That might be sufficient for the example in the OP.