Finding palindromes in 10+ languages
I wanted to find some palindromes. So I went digging for a dictionary on my laptop and found a few. There's one in particular which is located at
/usr/share/dict/words that is also a linux standard. It's plain text and newline seperated which means easy access. Let's do this.
That worked and found 197 palindromes as verified by
| wc -l. But was it fast? My roommate said he thinks python would be faster.
Python (Credit to gkze for making it more pythonic)
Turns out they're pretty even if you factor cpu:
Node 0.11.13: 0.18s user 0.03s system 100% cpu 0.213 total Python 2.7.8: 0.18s user 0.06s system 95% cpu 0.244 total
What about a bash script?
Holy hell was that slow. I only let it finish once and it was minutes of disapointment.
Update: Kenan Kalajdzic sent me the following one-liner that runs in less than half a second!
But wait, I bet Go is way faster!
I was correct, Golang runs about twice* as fast as Node and Python:
Golang 1.3.1: 0.11s user 0.01s system 101% cpu 0.112 total Node 0.11.13: 0.18s user 0.03s system 100% cpu 0.213 total Python 2.7.8: 0.18s user 0.06s system 95% cpu 0.244 total
* I've since optimized the Go code down to half of this!
Always keep learning
At this point I was interested and wrote it in six more languages. The format of implementing the same logic is great for getting a grasp on a new language very quickly. It was my first time for many of them and I must say a great learning experience.
I won't go through every language for the sake of brevity here were my results after just a day:
Clang 600.0.51: 0.03s user 0.00s system 96% cpu 0.031 total Rust 0.12.0: 0.03s user 0.00s system 95% cpu 0.036 total GO 1.3.1: 0.11s user 0.01s system 101% cpu 0.112 total Ruby 2.0.0: 0.15s user 0.01s system 99% cpu 0.154 total Haskell 7.8.3: 0.20s user 0.01s system 99% cpu 0.211 total Node 0.11.13: 0.18s user 0.03s system 100% cpu 0.213 total Python 2.7.8: 0.18s user 0.06s system 95% cpu 0.244 total Java 1.6.0: 0.35s user 0.04s system 154% cpu 0.253 total Scala 2.11.2: 0.67s user 0.08s system 138% cpu 0.538 total Bash 3.2.53: It's still running... :P
Optimizing the code
Over the day and going from beginning to end I progressed my view on how to solve the problem. By the end I was thinking about only changing the first character of the string where before it was just lowercase every word. The Rust implementation was originally slow until I added the optimization flag
rustc --opt-level=3 which made it jump up to less that 1/20th of a second.
See all the code, compiler settings and more stats on GitHub!
I've added even more languages and built a benchmarking tool to make comparisons easier.
Here's a sample of the updated results: