Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Did not work on benchmarking function with some non alphanumerics

by Anonymous Monk
on Jan 14, 2022 at 23:39 UTC ( #11140457=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Got error and not work on benchmarking function with some non alphanumerics such space like this
use Benchmark 'cmpthese' use strict; sub strMulti{return $_[0]x3} sub strConcat{return $_[0].$_[0].$_[0]}; sub cmpstr{ cmpthese(-2, { 'string multiplier', "print &strMulti($_[0])", 'string concat', "print &strConcat($_[0])" } ) } &cmpstr('foo bar') Can't locate object method "foo" via package "bar" (perhaps you forgot + to load "bar"?) at (eval 176)

and very likely many more failure on passing many other than alphanumeric characters
also got error but can't reproduce the exact issue, just sure kinda this
runloop unable to compile '&cmpstr( <html lang="en"> <body class="no-script">

... when it contains a HTML
Please help out solving the benchamark to be able compare such needs, thanks before

Replies are listed 'Best First'.
Re: Did not work on benchmarking function with some non alphanumerics
by Fletch (Bishop) on Jan 15, 2022 at 00:04 UTC

    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140457]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-05-21 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?