Given the simple programming problem of testing two words of being anagrams
I've found my simple solution I did a few years back. I decided to rework
it into a much simpler solution using Python's Counter collection.
But then I tried measuring the performance and was rather surprised.
Given the GIST below one can see three different attempts to the problem.
anagram_1(...) builds up two dicts of letters with their counts in the two words
and then compare the two dicts for similarity. If they're similar, the two
words are anagrams.
anagram_2(...) does the same thing but uses the python collection Counter to
do the counting for you.
anagram_3(...) had me try the first solution but using default dicts instead of just
getting the value from the dict first and adding one as well as using zip to iterate
over the two strings.
I was surprised to find the shorter anagram_2 to be about twice as slow as the
more naive approaches of maintaining the counts in the dicts while iterating over
each character in the strings.
Best out of 5 per loop
So simpler or more concise code isn't always the better performing type. 🤔