Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Double the speed of Imager->setpixel

by swl (Parson)
on Dec 11, 2022 at 22:55 UTC ( #11148753=note: print w/replies, xml ) Need Help??


in reply to Double the speed of Imager->setpixel

Setting a flag on the object to not run validity checks is one option, and safer than a global variable.

Imager is on GitHub at https://github.com/tonycoz/imager so you could always raise an issue and/or submit a pull request.

  • Comment on Re: Double the speed of Imager->setpixel

Replies are listed 'Best First'.
Re^2: Double the speed of Imager->setpixel
by hv (Prior) on Dec 12, 2022 at 00:43 UTC

    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.

      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.

Re^2: Double the speed of Imager->setpixel
by Anonymous Monk on Dec 12, 2022 at 19:28 UTC
    > Setting a flag on the object to not run validity checks is one option, and safer than a global variable.

    OP here, thanks for the replies. By "global" I meant to somehow disable all the slow sanity checks in Imager.pm. Methods like setpixel and getpixel are likely to be called a lot inside loops. Disabling the call to _valid_image on each pixel seems pretty safe so far and is a big gain. Since _valid_image is called in almost every method in Imager.pm they can all be fixed on line 658:

    sub _valid_image { return 1;
    The second hack that skips the call to _color is crude because it disables dwim on the color attribute of setpixel (color=>'blue') and requires it be supplied with an Imager::Color object (color=>Imager::Color->new('blue')). Doing that right would require more work but it hasn't broken my code so far while patching a live copy of Imager.pm. Probably because it was noticed long ago that dwim colors are to be avoided because setpixel is 2-3x faster when using color objects with rgb arrays like Imager::Color->new([255,0,0]).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2023-12-06 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (30 votes). Check out past polls.

    Notices?