Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

There are two fast ordered-hash implementationson on CPAN: Hash::Ordered and MCE::Shared::Ordhash.

use strict; use warnings; use Hash::Ordered; use Data::Dumper; tie my %table, 'Hash::Ordered'; for my $key (qw( a_a b_b c_c d_d e_e )) { $table{$key}{'pend'} += 1; $table{$key}{'run'} += 2; } print Dumper(\%table), "\n";

The following does the same thing via MCE::Shared::Ordhash (non-shared construction via Tie).

use strict; use warnings; use MCE::Shared::Ordhash; use Data::Dumper; tie my %table, 'MCE::Shared::Ordhash'; for my $key (qw( a_a b_b c_c d_d e_e )) { $table{$key}{'pend'} += 1; $table{$key}{'run'} += 2; } print Dumper(\%table), "\n";

Both produce the following output. Notice how the first level keys have retained order.

$VAR1 = { 'a_a' => { 'run' => 2, 'pend' => 1 }, 'b_b' => { 'pend' => 1, 'run' => 2 }, 'c_c' => { 'run' => 2, 'pend' => 1 }, 'd_d' => { 'run' => 2, 'pend' => 1 }, 'e_e' => { 'run' => 2, 'pend' => 1 } };

These modules are reasonably fast. The OO interface is faster when extra performance is desired. However, the TIE interface is nice when wanting the native hash look and feel. There's no reason why one cannot have both. MCE::Shared::Ordhash, via the overload mechanism, handles on-demand hash-dereferencing on the fly.

use strict; use warnings; use MCE::Shared::Ordhash; use Data::Dumper; my $table = MCE::Shared::Ordhash->new(); for my $key (qw( a_a b_b c_c d_d e_e )) { $table->{$key}{'pend'} += 1; $table->{$key}{'run'} += 2; } print Dumper($table), "\n";

The table hash is the real MCE::Shared::Ordhash object and not something hidden behind the TIE interface. Therefore, a dump of it will give you the structure of the object.

$VAR1 = bless( [ { 'a_a' => { 'run' => 2, 'pend' => 1 }, 'b_b' => { 'pend' => 1, 'run' => 2 }, 'd_d' => { 'run' => 2, 'pend' => 1 }, 'c_c' => { 'pend' => 1, 'run' => 2 }, 'e_e' => { 'pend' => 1, 'run' => 2 } }, [ 'a_a', 'b_b', 'c_c', 'd_d', 'e_e' ], {}, \0, \0, { 'a_a' => $VAR1->[0]{'a_a'}, 'b_b' => $VAR1->[0]{'b_b'}, 'c_c' => $VAR1->[0]{'c_c'}, 'd_d' => $VAR1->[0]{'d_d'}, 'e_e' => $VAR1->[0]{'e_e'} } ], 'MCE::Shared::Ordhash' );

If you have time, check them out. Do random deletes or anything pertaining to a hash. You will be pleased with the performance. In that case, one might find the following useful.

use strict; use warnings; use Hash::Ordered; use MCE::Shared::Ordhash; use List::Util 'shuffle'; use Time::HiRes 'time'; my ($start, $total); srand 0; sub ready { my $time = time; $total += $time - $start; $start = $time; } # my $oh = Hash::Ordered->new(); my $oh = MCE::Shared::Ordhash->new(); print ref($oh), "\n"; my @keys1 = shuffle('aaaa'..'gggf'); # size: 109,674 my @keys2 = shuffle('gggg'..'mmmm'); # size: 109,675 my @keys3 = shuffle('nnnn'..'ttts'); # size: 109,674 my @keys4 = shuffle('tttt'..'zzzz'); # size: 109,675 $oh->set($_,$_) for ('a'..'m','_','n'..'z'); # add 26 or more keys $oh->delete('_'); # has INDX afterwards $start = time; $oh->set($_,$_) for @keys1; printf "duration (set ): %0.02f\n", time - $start; ready(); $oh->push($_,$_) for @keys2; printf "duration (push ): %0.02f\n", time - $start; ready(); $oh->unshift($_,$_) for @keys3; printf "duration (unshift): %0.02f\n", time - $start; ready(); $oh->merge(map {$_,$_} @keys4); printf "duration (merge ): %0.02f\n", time - $start; # ready(); # $oh->delete($_) for @keys2; # printf "duration (delete ): %0.02f\n", time - $start; printf "total time : %0.02f\n", $total += time - $start;

Results vary from system to system. The following were captured from a 2.6 GHz machine. Anyway, these are the fastest pure-Perl ordered-hash implementations on CPAN right now. The results show the time taken to complete each action. Thus, lower is faster.

Hash::Ordered duration (set ): 0.20 duration (push ): 0.24 duration (unshift): 0.29 duration (merge ): 0.23 total time : 0.95 MCE::Shared::Ordhash duration (set ): 0.12 duration (push ): 0.16 duration (unshift): 0.21 duration (merge ): 0.15 total time : 0.65

MCE::Shared::Ordhash has low overhead. The reason is that the internal INDX hash is populated only on-demand. There are no worries about delays, for example deleting keys. Compare these with Tie::IxHash. Do forward and reverse deletes. Not to forget random deletes.

Perl is fun.


In reply to Re^3: How to add columns with new row name using perl from mysql query? by Anonymous Monk
in thread How to add columns with new row name using perl from mysql query? by perlanswers

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2020-10-25 14:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (249 votes). Check out past polls.

      Notices?