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

Re: BWTDI: Better Way To Do It?

by dkubb (Deacon)
on Jan 15, 2001 at 12:48 UTC ( [id://51878]=note: print w/replies, xml ) Need Help??


in reply to BWTDI: Better Way To Do It?

I think I may have found a way that could be more efficient:
sub grab { my $class = shift; my $dice = shift; my $self = ref($class) || $class; unless ($dice =~ m/^(\d+)[Dd](\d+)([Ss])?$/ ) { croak "Invalid dice format to $self->grab: $dice"; } #$1 - Quantity #$2 - Type #$3 - Face my @dice = map { Die->get( $2, $3 ? 1 : 0 ) } 1..$1; return bless { dice => \@dice }, $self; }

Here's a summary of what I did:

I changed the code to be more in-line, as there is no need for a logic branch if the regex fails to return a match. Inside the croak command, I figured it was better to report back the passed in $dice, rather than $!, which is generaly set by system-level errors.

The regex was optimized. The /i modifier wasn't necessary because you were wanting to match d and D, using a character class is faster anyhow, as in: [Dd]. Same with the [Ss] on the end, which was made an optional match, rather than use the (.*) or (.*s). The Mastering Regex book cautions against using the /i when a character class will do just as well.

Instead of creating temporary variables, I used the $1, $2, $3 variables directly. Of course, I compensated for the loss in readability, by using comments =).

I could have pushed onto an array inside a loop, but map was used and the values were copied directly onto the array. I benchmarked using for, foreach and map, with map coming out as a winner in my tests. The others won when I experimented with making $1 equal to 10000 or so, but this will most likely not get this high, in the real-world.

Also, you'll notice that $3 ? 1 : 0 was used, it's not possible to do something like $3 &&= 1, since $3 is a read-only variable.

Replies are listed 'Best First'.
Re: BWTDI: And the winner is dkubb
by coreolyn (Parson) on Jan 15, 2001 at 21:12 UTC

    Great post! Thought you might want to see some results:

    Dice Version .04 IO
    OneDiceNotStored: 3 wallclock secs ( 4.43 usr + 0.00 sys = 4.43 C +PU) OneFacedDiceStored: 5 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 C +PU)
    Dice Version .05 dkubb
    OneDiceNotStored: 4 wallclock secs ( 4.39 usr + 0.00 sys = 4.39 C +PU) OneFacedDiceStored: 6 wallclock secs ( 5.24 usr + 0.00 sys = 5.24 C +PU)
    Now to go back and update Dice::Dice, but with all these examples I think I'm going to revisit Dice::Die again too.

    coreolyn

Log In?
Username:
Password:

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

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

    No recent polls found