http://qs321.pair.com?node_id=11140458


in reply to Did not work on benchmarking function with some non alphanumerics

Your "code" to compare is inside double quotes so things like $_[0] are going to be interpolated in before the value is passed in to cmpthese. You'd want to use single quotes, or better provide a code ref.

sub cmpstr { my( $arg ) = @_; cmpthese( -2, 'string multiplier', sub { print strMulti( $arg ) }. 'string concat', sub { print strConcat( $arg ) }, ); }

That aside be aware that your two subs do different things; strConcat will return a single string consisting of three copies of the argument, while strMulti will return three copies of the argument as a list. I misread as pointed out below (I was seeing parens in there ($_[0])x3 that aren't there . . .).

Edit: Thirdly, don't prefix subroutine calls with an ampersand. There's rare corner cases where you need to do that (disabling prototypes) but in general it's unneeded and makes your code harder to read (and looks like you're still writing perl4).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Did not work on benchmarking function with some non alphanumerics
by jwkrahn (Abbot) on Jan 15, 2022 at 05:16 UTC
    while strMulti will return three copies of the argument as a list.

    SCALAR x 3 will return a scalar while LIST x 3 will return a list.

    So the two subs do basicaly the same thing.