Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: OOP - Constant Vs. Subroutine

by BrowserUk (Patriarch)
on May 12, 2007 at 15:55 UTC ( [id://615087]=note: print w/replies, xml ) Need Help??


in reply to Re: OOP - Constant Vs. Subroutine
in thread OOP - Constant Vs. Subroutine

You pays your money and takes your choice:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use constant { X => 0, Y => 1, Z => 2, P => 3, Q => 4, }; use Readonly; Readonly::Hash my %Fields => { X => 0, Y => 1, Z => 2, P => 3, Q => 4, }; sub prioritisedSort1 { return $a->[ P ] <=> $b->[ P ] or $a->[ Z ] <=> $b->[ Z ] or $a->[ Y ] <=> $b->[ Y ] or $a->[ X ] <=> $b->[ X ] or $a->[ Q ] <=> $b->[ Q ]; } sub prioritisedSort2 { return $a->[$Fields{P}] <=> $b->[$Fields{P}] or $a->[$Fields{Z}] <=> $b->[$Fields{Z}] or $a->[$Fields{Y}] <=> $b->[$Fields{Y}] or $a->[$Fields{X}] <=> $b->[$Fields{X}] or $a->[$Fields{Q}] <=> $b->[$Fields{Q}]; } our $N ||= 1e5; my @AoA1 = map { [ map{ int rand $_ } 1000, 100, 10, 500, 50 ] } 1 .. $N; my @AoA2 = map{ [ @$_ ] } @AoA1; my( @sorted1, @sorted2 ); cmpthese -1, { constant => sub{ @sorted1 = sort prioritisedSort1 @AoA1 }, Readonly => sub{ @sorted2 = sort prioritisedSort2 @AoA2 }, }; print "\nFirst and last 5 for constant"; print "[@$_]" for @sorted1[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted1[ -5 .. -1 ]; print "\nFirst and last 5 for Readonly"; print "[@$_]" for @sorted2[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted2[ -5 .. -1 ]; __END__ C:\test>junk8 -N=1e2 Rate Readonly constant Readonly 137/s -- -96% constant 3459/s 2427% -- First and last 5 for constant [226 44 0 8 3] [425 64 7 10 2] [790 25 8 20 27] [461 37 0 27 47] [151 51 3 31 35] [- - - - -] [734 15 9 482 6] [90 40 2 489 5] [957 21 9 491 45] [17 99 3 494 44] [585 50 7 495 22] First and last 5 for Readonly [226 44 0 8 3] [425 64 7 10 2] [790 25 8 20 27] [461 37 0 27 47] [151 51 3 31 35] [- - - - -] [734 15 9 482 6] [90 40 2 489 5] [957 21 9 491 45] [17 99 3 494 44] [585 50 7 495 22]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found