Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Does anyone know what the performance penalty of utf8 hash keys -- even if they contain only ASCII chars -- is?
Benchmarking script:
use strict; use warnings; use Encode qw( _utf8_on ); use Benchmark::Timer; use Devel::Peek; my $timer = Benchmark::Timer->new; my $latin1 = "foo"; my $utf8 = "foo"; _utf8_on($utf8); my %latin1_hash = ( $latin1 => 1 ); my %utf8_hash = ( $utf8 => 1 ); warn "===================================\n"; warn "Latin-1 hash key:\n----------------------------------\n"; Dump \%latin1_hash; warn "===================================\n"; warn "UTF-8 hash key:\n-----------------------------------\n"; Dump \%utf8_hash; warn "===================================\n"; for my $iter ( 1 .. 10 ) { my $total = 0; $timer->start("Latin-1 hash key, Latin-1 probe"); $total += $latin1_hash{$latin1} for 1 .. 1_000_000; $timer->stop("Latin-1 hash key, Latin-1 probe"); $total = 0; $timer->start("Latin-1 hash key, UTF-8 probe"); $total += $utf8_hash{$latin1} for 1 .. 1_000_000; $timer->stop("Latin-1 hash key, UTF-8 probe"); $total = 0; $timer->start("UTF-8 hash key, Latin-1 probe"); $total += $latin1_hash{$utf8} for 1 .. 1_000_000; $timer->stop("UTF-8 hash key, Latin-1 probe"); $total = 0; $timer->start("UTF-8 hash key, UTF-8 probe"); $total += $utf8_hash{foo} for 1 .. 1_000_000; $timer->stop("UTF-8 hash key, UTF-8 probe"); } warn scalar $timer->reports . "\n";
Results for vanilla custom-compiled Perl 5.10.0 on Mac OS X:
marvin@smokie:~/perltest $ perl hash_key_bench.pl =================================== Latin-1 hash key: ---------------------------------- SV = RV(0x8503ac) at 0x8503a0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x84df80 SV = PVHV(0x809048) at 0x84df80 REFCNT = 2 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x200f60 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "foo" HASH = 0x238678dd SV = IV(0x85036c) at 0x850370 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 =================================== UTF-8 hash key: ----------------------------------- SV = RV(0x8503ac) at 0x8503a0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x84df40 SV = PVHV(0x80905c) at 0x84df40 REFCNT = 2 FLAGS = (PADMY,SHAREKEYS,HASKFLAGS) ARRAY = 0x200f80 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "foo" [UTF8 "foo"] HASH = 0x238678dd SV = IV(0x85038c) at 0x850390 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 =================================== Results: ----------------------------------- 10 trials of Latin-1 hash key, Latin-1 probe (2.272s total), 227.159ms +/trial 10 trials of Latin-1 hash key, UTF-8 probe (2.248s total), 224.823ms/t +rial 10 trials of UTF-8 hash key, Latin-1 probe (3.100s total), 309.985ms/t +rial 10 trials of UTF-8 hash key, UTF-8 probe (1.893s total), 189.301ms/tri +al

In reply to Re: Unexpected utf8 in hash keys by creamygoodness
in thread Unexpected utf8 in hash keys by kappa

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!
  • 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 browsing the Monastery: (18)
    As of 2015-07-07 17:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (92 votes), past polls