Smart match versus hash deathmatch

A couple of days ago, I posted about my answer to a Stackoverflow question asking about the speed of the smart match operator. Smart matches looking for a scalar in an array, can short circuit, so they are pretty speedy.

The trick, however, is if this sort of smart match is faster than a hash lookup. Yes they are and no they aren't. I've updated my Stackoverflow answer with additional benchmarks and a new plot.

Smart matches are faster if you have to create the hash, but slower if you already have the hash.

There's a middle ground in between that I don't care to find. For some number of searches of the hash, the cost of creating the hash amortizes enough that it's faster than a smart match.

It depends on what you are doing, but that's the rub with every benchmark. The numbers aren't the answers to your real question, in this case "Which technique should I use?". They only support a decision once you add context.


Out of curiosity, why aren't you posting the results here and directing those on stackoverflow to this blog?

Thanks - part of me has been wondering where people are talking about Perl now with the lack of write access to

Perhaps I should clarify a bit more. I've started to notice a trend of syndication, the first instance being with I would see tweets which link to, which would have a (almost hidden) link to follow to the actual content.

So I've been trying to get a better understanding of why people are syndicating, and if they are posting abridged versions on the intermediate sites (in this case, posterous in other cases), and why they are doing that instead of just linking directly to the end site (in this case stackoverflow) with the content.

Normally I would have expected to see an answer like this posted on, but that site is so slow that I don't visit it anymore. I was wondering why you chose stackoverflow - and I guess the answer is that is where the question was posted in the first place. Part of me is curious about the psychology of why more Perl Q&A isn't here on, but it seems like stackoverflow is growing in popularity, and more suited to this type of conversation.

Hope that makes sense :)

Sorry for getting off topic with my comments - scratched an itch that didn't belong here.

On topic though, thanks for posting this - good read. Interesting that the search is context dependent, but makes sense when I think about it. If you have to repeatedly search for an element, it might be worth ordering the hash specifically for the search you are running so that you're always looking in the beginning of the dataset, assuming the cost of ordering is relatively small.

Leave a comment

About brian d foy

user-pic I'm the author of Mastering Perl, and the co-author of Learning Perl (6th Edition), Intermediate Perl, Programming Perl (4th Edition) and Effective Perl Programming (2nd Edition).