Faster ordered hashes for Perl

Reading time: 2 minutes

With some prompting and suggestions from Mario Roy, author of MCE, I’ve been optimizing Hash::Ordered. With the exception of setting existing/new elements, which got a bit slower due to ensuring keys are strings, not references, most functions got faster. Some, like large hash deletion, are now MUCH faster.

Here are changes in benchmarks from version 0.002 to 0.009:

$VERSION      0.002      0.009

     Results for ordered hash creation for 10 elements

                    94121/s   101293/s

     Results for ordered hash creation for 100 elements

                    10931/s    11226/s

     Results for ordered hash creation for 1000 elements

                     1022/s     1160/s

     Results for fetching ~10% of 10 elements

                  1417712/s  1844781/s

     Results for fetching ~10% of 100 elements

                   244800/s   285983/s

     Results for fetching ~10% of 1000 elements

                    24871/s    30342/s

     Results for replacing ~10% of 10 elements

                  1353795/s  1378880/s

     Results for replacing ~10% of 100 elements

                   197232/s   192769/s

     Results for replacing ~10% of 1000 elements

                    20364/s    19909/s

     Results for adding 10 elements to empty hash

                   367588/s   341022/s

     Results for adding 100 elements to empty hash

                    66495/s    58519/s

     Results for adding 1000 elements to empty hash

                     7217/s     6497/s

     Results for creating 10 element hash then deleting ~10%

                    95284/s   94598/s

     Results for creating 100 element hash then deleting ~10%

                     6924/s    9242/s

     Results for creating 1000 element hash then deleting ~10%

                      144/s     934/s

     Results for listing pairs of 10 element hash

                   170187/s   178288/s

     Results for listing pairs of 100 element hash

                    18839/s    19537/s

     Results for listing pairs of 1000 element hash

                     1877/s     1959/s

You can see Hash::Ordered benchmarked against other modules in Hash::Ordered::Benchmarks.

If you need an ordered hash, I encourage you to try Hash::Ordered.

•      •      •

If you enjoyed this or have feedback, please let me know by or