Why go through the process of having to convert text to a hash at runtime, everytime; and load all languages for every run.

Putting each language in its own file would certainly be an improvement - I grant you that.

However, converting text to the hash at run time is actually *faster* than hardcoding the hash, at least for simple data.

Yes, that's right. This:

my %hash; while (<DATA>) { my ($k, $v) = split /\t/o; $hash{$k} = $v; } __DATA__ 440035528809 6946395707444 332679554392 162874763688655 913537320343 56726180700920

is faster than this:

my %hash = ( 440035528809=>'6946395707444', 332679554392=>'162874763688655', 913537320343=>'56726180700920', );

Or at least it is once you've got more than a few hundred entries in the hash.

It seems counter-intuitive, but it makes sense when you think about it. In the first example we're parsing a very simple text format using Perl (and Perl is very fast at text handling!); in the second we're parsing a programming language using C.

If you're interested in benchmarks, the following script generates two Perl scripts called perl.pl and data.pl:

use 5.010; open my $perl, '>', 'perl.pl'; open my $data, '>', 'data.pl'; print $perl <<'CODE'; use strict; my %hash = ( CODE print $data <<'CODE'; use strict; my %hash; while (<DATA>) { my ($k, $v) = split /\t/o; $hash{$k} = $v; } __DATA__ CODE for (0 .. 100_000) { my $k = int rand 1_000_000_000_000; my $v = int rand 1_000_000_000_000_000; print $perl "$k=>'$v',\n"; print $data "$k\t$v\t\n"; } print $perl <<'CODE'; ); CODE

In my tests, data.pl (which reads data from __DATA__) is about 40% faster than perl.pl.

I did quite a bit of benchmarking on this sort of thing for Crypt::XkcdPassword.


In reply to Re^2: Text storage/retrieval by tobyink
in thread Text storage/retrieval by DreamT

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, details, 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, summary, 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.