Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following map variant works out significantly faster than the previously posted map variants, though not as fast as for-loop based variants:

sub variation3ep { my $idx = -1; my %hash = map(($_ => ++$idx), @array); }

While the block form of map is often favoured for its clarity, the expression form of map is faster because it avoids the overhead of creating a lexical pad.

This is actually another reason why BrowserUk's variation4 is fast - he uses the for statement modifier rather than a for loop with a block. Compare:

#!/usr/bin/env perl use strict; use warnings; use Benchmark qw(:all); my @array='aa' .. 'zz'; sub variation4 { my $idx = 0; my %hash; $hash{ $_ } = $idx++ for @array; } sub variation5 { my $idx = 0; my %hash; for (@array) { $hash{ $_ } = $idx++; } } cmpthese(-3, { 'variation4' => \&variation4, 'variation5' => \&variation5, }); __END__ Rate variation5 variation4 variation5 740/s -- -4% variation4 773/s 5% --

Yes, it's a small difference, but it's pretty consistently observable.

The absolute fastest I've been able to achieve is a small variation on BrowserUk's variation4 using the prefix increment rather than postfix increment:

sub variation6 { my $idx = -1; my %hash; $hash{ $_ } = ++$idx for @array; }

It seems to give you about a 6% speed up.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^5: better array to hash conversion by tobyink
in thread better array to hash conversion by perltux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-18 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found