in reply to Re: Does Perl do constant anonymous hash creation optimisation?
in thread Does Perl do constant anonymous hash creation optimisation?

Thanks for that - have never really used
use constant {...};
we usually do this at the top of the package
our $SKIPWORDS = {};
so I wonder if there is an overhead for use constant?

The thing I like about the inline/anon hashes is that all the logic and data is clearly seen in one place.

In this specific example, the package is 5000+ lines long, and the words() function because it starts with w... will end up near the end of the file - 5000 lines away from the skip and substitute words.

Will probably go with the use contant if there is no overhead - this func will get called a lot in an inner loop with anywhere up to a million times per run.

Thanks again, for the pointers,

Jeff

Replies are listed 'Best First'.
Re^3: Does Perl do constant anonymous hash creation optimisation?
by BrowserUk (Patriarch) on Jul 08, 2006 at 15:53 UTC

    Using constant is 3 times faster than your inline construct.

    In this specific example, the package is 5000+ lines long, and the words() function because it starts with w... will end up near the end of the file - 5000 lines away from the skip and substitute words.

    With that much data, I'd definitely be putting it in a separate file on it's own. If you do not want to go to the bother of wrapping it up as a module, you could just put the constant hash definition into a file of it's own and use the simple do 'wordshash.pl'; just before the associated words() functions, though it would probably be better to wrap the function and data into a module and do something like use My::Words qw[ words ]; in the main code.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Fab! Thanks very much!

      I am going to keep this to remind me how to performance test my funcs better in the future.

      Regards,

      Jeff

Re^3: Does Perl do constant anonymous hash creation optimisation?
by jaa (Friar) on Jul 10, 2006 at 15:06 UTC
    Dear Monks,

    Extending the cmpthese() example, I found no appreciable performance difference between:

    use constant SKIPWORDS => {...}; our $SKIPWORDS = {};
    So a big thumbs up for use constant()!

    Thanks for the help!

    Jeff