.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Hash::Ordered::Benchmarks 3" .TH Hash::Ordered::Benchmarks 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Hash::Ordered::Benchmarks \- Ordered hash benchmarking .SH VERSION .IX Header "VERSION" version 0.014 .SH INTRODUCTION .IX Header "INTRODUCTION" The Hash::Ordered internals are simple: a hash of data and an array of ordered keys. I thought this would perform well for common tasks and likely outperform more complicated ordered hash implementations, so I decided to do some benchmarking to test it. .PP \&\fBNote\fR: since the initial benchmarking, \f(CW\*(C`Hash::Ordered\*(C'\fR gained just-in-time indexing of the keys array to support faster tombstone deletion, which adds some conditional data structures to the internals. It also now supports \f(CW\*(C`tie\*(C'\fR. The revised benchmarks include the \f(CW\*(C`tie\*(C'\fR mode for comparison with other tied hash implementations. .SH "MODULES TESTED" .IX Header "MODULES TESTED" In my review of alternatives to \f(CW\*(C`Hash::Ordered\*(C'\fR, six seemed sufficiently general-purpose to be worth benchmarking. The modules tested are listed in the benchmark output in shorthand: .IP \(bu 4 Array::AsHash — denoted \f(CW\*(C`a:ah\*(C'\fR .IP \(bu 4 Array::OrdHash — denoted \f(CW\*(C`a:oh\*(C'\fR .IP \(bu 4 Data::XHash — denoted \f(CW\*(C`d:xh\*(C'\fR .IP \(bu 4 Hash::Ordered — denoted \f(CW\*(C`h:o\*(C'\fR and marked with "*" .IP \(bu 4 Tie::Hash::Indexed — denoted \f(CW\*(C`t:h:i\*(C'\fR .IP \(bu 4 Tie::IxHash — denoted \f(CW\*(C`t:ix\*(C'\fR .IP \(bu 4 Tie::LLHash — denoted \f(CW\*(C`t:llh\*(C'\fR .PP Note that Tie::Hash::Indexed is written in XS and also may require forced installation as its tests often fail for Perl 5.18+ due to the hash randomization change. .PP If there are different methods of doing something with a module, the variations are described in each section below. .SH BENCHMARKS .IX Header "BENCHMARKS" I conducted benchmarking with the Benchmark module. The test script is in the \f(CW\*(C`devel\*(C'\fR directory of the distribution. Tests were run on Perl 5.20.2 on a Mac Book Pro (darwin\-thread\-multi\-2level). Each benchmark ran for 5 CPU seconds. .PP Benchmarks were run at several different scales to reveal differences in efficiency as hash size grows. The details are described in each section below. .PP A seed list of keys and values was generated from random integers using Math::Random::MT::Auto. The same seed list was used for all benchmarks unless otherwise noted. .PP I did not test advanced features of these modules, as apples-to-apples comparison is difficult. Still, the performance on common, simple measures could suggest how features that combine these operations might perform. .SS "Ordered hash creation" .IX Subsection "Ordered hash creation" I tested hash creation for 10, 100 and 1000 elements. For some modules there were different options for creating a hash: .IP \(bu 4 \&\f(CW\*(C`Array::AsHash\*(C'\fR takes an array-reference with an option to use it directly or to clone it. In one case I provided the seed list array reference with the clone option to true ("a:ah_cp"). In another case I created a new array reference from the seed list and provided it directly ("a:ah_rf"). .IP \(bu 4 \&\f(CW\*(C`Hash::Ordered\*(C'\fR can be initialized either with \f(CW\*(C`new\*(C'\fR ("h:o_oo") or via \f(CW\*(C`tie\*(C'\fR ("h:o_th"). .IP \(bu 4 \&\f(CW\*(C`Tie::IxHash\*(C'\fR can be initialized either with \f(CW\*(C`new\*(C'\fR ("t:ix_oo") or via \f(CW\*(C`tie\*(C'\fR ("t:ix_th"). .IP \(bu 4 \&\f(CW\*(C`Data::XHash\*(C'\fR can be created with a list ("t:xh_ls") or an array reference ("t:xh_rf"). .PP As expected, when \f(CW\*(C`Array::AsHash\*(C'\fR gets an array reference, it's very fast. \&\f(CW\*(C`Tie::Hash::Indexed\*(C'\fR does well here, also. Of the non-XS, more hash-like choices, \f(CW\*(C`Hash::Ordered\*(C'\fR does well. .PP .Vb 12 \& Results for ordered hash creation for 10 elements \& t:h:i 136030/s \& a:ah_rf 111411/s \& h:o_oo 101293/s * \& h:o_th 98646/s * \& t:ix_oo 61853/s \& t:ix_th 61715/s \& a:ah_cp 56375/s \& a:oh 54337/s \& t:llh 33553/s \& d:xh_ls 14068/s \& d:xh_rf 13926/s \& \& Results for ordered hash creation for 100 elements \& t:h:i 16503/s \& a:ah_rf 15398/s \& h:o_oo 11226/s * \& h:o_th 10793/s * \& a:oh 7783/s \& t:ix_th 7570/s \& t:ix_oo 7405/s \& a:ah_cp 7035/s \& t:llh 3533/s \& d:xh_ls 1561/s \& d:xh_rf 1550/s \& \& Results for ordered hash creation for 1000 elements \& t:h:i 1552/s \& a:ah_rf 1509/s \& h:o_oo 1160/s * \& h:o_th 1158/s * \& a:oh 815/s \& t:ix_th 772/s \& t:ix_oo 757/s \& a:ah_cp 684/s \& t:llh 340/s \& d:xh_ls 154/s \& d:xh_rf 152/s .Ve .SS "Getting hash elements" .IX Subsection "Getting hash elements" I tested retrieving values for 10% of the keys, randomly selected, from hashes of 10, 100 and 1000 elements. The hash was created beforehand so the benchmarks reflect only element access. .PP Some modules had choices for how to retrieve an value, usually between a method (denoted with "_oo"), tied hash access ("_th") or with a dereference ("_rf"). .PP Generally, method calls turned out faster than other approaches for a given module, demonstrating the inefficiency of tied objects. .PP .Vb 11 \& Results for fetching ~10% of 10 elements \& h:o_oo 1844781/s * \& d:xh_oo 1292883/s \& t:ix_oo 1187104/s \& t:h:i 932793/s \& h:o_th 817346/s * \& d:xh_rf 703441/s \& t:ix_th 649291/s \& a:oh 560060/s \& t:llh 514911/s \& a:ah 260639/s \& \& Results for fetching ~10% of 100 elements \& h:o_oo 285983/s * \& d:xh_oo 183292/s \& t:ix_oo 165100/s \& t:h:i 128713/s \& h:o_th 107213/s * \& d:xh_rf 87049/s \& t:ix_th 79642/s \& a:oh 66109/s \& t:llh 58741/s \& a:ah 27533/s \& \& Results for fetching ~10% of 1000 elements \& h:o_oo 30342/s * \& d:xh_oo 19004/s \& t:ix_oo 17132/s \& t:h:i 13269/s \& h:o_th 11100/s * \& d:xh_rf 8919/s \& t:ix_th 7844/s \& a:oh 6763/s \& t:llh 5666/s \& a:ah 2772/s .Ve .SS "Setting hash elements" .IX Subsection "Setting hash elements" I tested changing values for 10% of the keys, randomly selected, from hashes of 10, 100 and 1000 elements. The hash was created beforehand so the benchmarks reflect only element mutation. No new keys were added. .PP Some modules had choices for how to modify a value, usually between a method (denoted with "_oo"), tied hash access ("_th") or with a dereference ("_rf"). .PP Again, methods outperformed. .PP .Vb 11 \& Results for replacing ~10% of 10 elements \& h:o_oo 1378880/s * \& t:h:i 945403/s \& d:xh_oo 941643/s \& t:ix_oo 887283/s \& h:o_th 652269/s * \& t:llh 590160/s \& d:xh_rf 537694/s \& a:oh 530787/s \& t:ix_th 508001/s \& a:ah 159258/s \& \& Results for replacing ~10% of 100 elements \& h:o_oo 192769/s * \& t:h:i 126284/s \& d:xh_oo 119845/s \& t:ix_oo 113992/s \& h:o_th 81159/s * \& t:llh 72403/s \& d:xh_rf 64791/s \& a:oh 62666/s \& t:ix_th 59809/s \& a:ah 16405/s \& \& Results for replacing ~10% of 1000 elements \& h:o_oo 19909/s * \& t:h:i 13445/s \& d:xh_oo 12487/s \& t:ix_oo 11601/s \& h:o_th 8357/s * \& t:llh 7503/s \& d:xh_rf 6599/s \& a:oh 6410/s \& t:ix_th 6118/s \& a:ah 1651/s .Ve .SS "Adding hash elements" .IX Subsection "Adding hash elements" I tested adding 10, 100 and 1000 elements to an empty hash. .PP Some modules had choices for how to append a value, usually between a method (denoted with "_oo"), tied hash access ("_th") or with a dereference ("_rf"). .PP For \f(CW\*(C`Tie::LLHash\*(C'\fR, I did not use the "lazy" option, but did the equivalent using \f(CW\*(C`tied\*(C'\fR and a method call: .PP .Vb 1 \& tied(%tllh)\->last( irand(), 42 ) for 1 .. $n; .Ve .PP Generally, it seemed like the differences were smaller than for other benchmarks. Methods still outperformed. .PP .Vb 11 \& Results for adding 10 elements to empty hash \& h:o_oo 341022/s * \& t:h:i 295079/s \& t:ix_oo 258981/s \& h:o_th 245996/s * \& t:ix_th 211341/s \& t:llh 191298/s \& a:oh 137447/s \& a:ah 112651/s \& d:xh_oo 87215/s \& d:xh_rf 80379/s \& \& Results for adding 100 elements to empty hash \& h:o_oo 58519/s * \& t:h:i 55166/s \& t:ix_oo 48658/s \& h:o_th 42066/s * \& t:ix_th 38632/s \& a:oh 34842/s \& t:llh 28384/s \& d:xh_oo 24841/s \& d:xh_rf 21517/s \& a:ah 13726/s \& \& Results for adding 1000 elements to empty hash \& h:o_oo 6497/s * \& t:h:i 6108/s \& t:ix_oo 5528/s \& h:o_th 4650/s * \& t:ix_th 4329/s \& a:oh 4233/s \& d:xh_oo 3121/s \& t:llh 3011/s \& d:xh_rf 2696/s \& a:ah 1423/s .Ve .SS "Deleting hash elements" .IX Subsection "Deleting hash elements" I tested creating hashes of 10, 100 and 1000 elements and then deleting 10% of the keys, chosen randomly. I would have liked to have isolated creation from deletion, but I couldn't figure out a way to do that given how \f(CW\*(C`Benchmark\*(C'\fR runs the same tests over and over. .PP Some modules had choices for how to delete a value, usually between a method (denoted with "_oo"), tied hash access ("_th") or with a dereference ("_rf"). .PP The performance changes (or lack thereof) at the three different sizes reveals implementation differences. (Though recall that some of this is the creation performance difference as well as deletion difference.) .PP For example, \f(CW\*(C`Tie::Hash::Indexed\*(C'\fR XS does very well, which could be its good creation performance, but could also be good deletion. .PP \&\f(CW\*(C`Hash::Ordered\*(C'\fR does linear search deleting a key for the 10 element hash, but automatically switches to indexed, tombstone deletion for the larger hashes. When deleting only 10% of keys, garbage collection of tombstoned keys never occurs, so that amortized cost is not included. .PP \&\f(CW\*(C`Tie::LLHash\*(C'\fR improves at larger sizes as deleting from a linked list is faster than splicing out an element of an array. Conversely, \f(CW\*(C`Array::AsHash\*(C'\fR just gets worse. .PP .Vb 11 \& Results for creating 10 element hash then deleting ~10% \& t:h:i 131578/s \& h:o_oo 94598/s * \& h:o_th 84018/s * \& a:ah 67109/s \& t:ix_oo 55477/s \& t:ix_th 52792/s \& a:oh 46938/s \& t:llh 30399/s \& d:xh_oo 13756/s \& d:xh_rf 13499/s \& \& Results for creating 100 element hash then deleting ~10% \& t:h:i 17420/s \& h:o_oo 9242/s * \& h:o_th 8438/s * \& a:oh 5738/s \& t:ix_oo 3922/s \& t:ix_th 3862/s \& a:ah 3286/s \& t:llh 3250/s \& d:xh_oo 1508/s \& d:xh_rf 1499/s \& \& Results for creating 1000 element hash then deleting ~10% \& t:h:i 1635/s \& h:o_oo 934/s * \& h:o_th 799/s * \& t:llh 319/s \& a:oh 204/s \& d:xh_oo 152/s \& d:xh_rf 151/s \& t:ix_oo 78/s \& t:ix_th 78/s \& a:ah 40/s .Ve .SS "Extracting the hash as a list" .IX Subsection "Extracting the hash as a list" I tested getting an ordered list of pairs from hashes of 10, 100 and 1000 elements. The hash was created beforehand so the benchmarks reflect only conversion to a list. .PP Oddly, modules that usually have more than one way to do things don't for this. Even \f(CW\*(C`Tie::IxHash\*(C'\fR doesn't really have an OO way to do it, so I did it longhand: .PP .Vb 1 \& @list = map { $_ => $tix_oo\->FETCH($_) } $tix_oo\->Keys; .Ve .PP Because \f(CW\*(C`Array::AsHash\*(C'\fR keeps its internal representation as an ordered list of pairs, it outperforms the rest handily as it merely needs to dereference that data structure. .PP .Vb 10 \& Results for listing pairs of 10 element hash \& a:ah 321044/s \& h:o_oo 178288/s * \& t:ix_oo 89263/s \& t:h:i 79184/s \& h:o_th 56112/s * \& t:ix_th 48009/s \& a:oh 47433/s \& t:llh 37996/s \& d:xh 37439/s \& \& Results for listing pairs of 100 element hash \& a:ah 36399/s \& h:o_oo 19537/s * \& t:ix_oo 9049/s \& t:h:i 7768/s \& h:o_th 6254/s * \& a:oh 5060/s \& t:ix_th 4907/s \& d:xh 4122/s \& t:llh 3813/s \& \& Results for listing pairs of 1000 element hash \& a:ah 3784/s \& h:o_oo 1959/s * \& t:ix_oo 905/s \& t:h:i 773/s \& h:o_th 625/s * \& a:oh 523/s \& t:ix_th 492/s \& d:xh 427/s \& t:llh 377/s .Ve .SH CONCLUSION .IX Header "CONCLUSION" With the exception of hash creation and element deletion, \f(CW\*(C`Hash::Ordered\*(C'\fR generally outperformed the other ordered hash implementations. Even for creation, it was the fastest of the pure-Perl, hash-based implementations, often by a large margin. .PP In the original release of \f(CW\*(C`Hash::Ordered\*(C'\fR, deletion got worse as the hash size grew. The new JIT indexing with tombstones now makes deletion far faster than any pure-Perl implementation. .PP \&\f(CW\*(C`Array::AsHash\*(C'\fR, with the opposite internal implementation compared to \&\f(CW\*(C`Hash::Ordered\*(C'\fR, performs best at creation and listing pairs, but is dead last at element access and modification. I believe the poor performance is mostly due to extra indirection (e.g. an extra function call) and logic in the element access methods. For uses that don't require much element access and have lots of creation/serialization, it could still be a useful choice. .PP Generally, every module that depends on \f(CW\*(C`tie\*(C'\fR for some portion of its implementation pays a substantial performance penalty. Comparing \&\f(CW\*(C`Hash::Ordered\*(C'\fR benchmarks with and without \f(CW\*(C`tie\*(C'\fR for individual element operations shows how severe this penalty can be. \f(CW\*(C`Tie::Hash::Indexed\*(C'\fR — likely because of its XS implementation — performs decently, but not well enough in my opinion to justify its use. .PP As the author of \f(CW\*(C`Hash::Ordered\*(C'\fR, I'm clearly biased, but I think these benchmarks make a very good case for it being the "go to" module for pure-Perl, general-purpose ordered hashes. .SH AUTHOR .IX Header "AUTHOR" David Golden .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is Copyright (c) 2014 by David Golden. .PP This is free software, licensed under: .PP .Vb 1 \& The Apache License, Version 2.0, January 2004 .Ve