3 ms·
Command line tool that ingests an arbitrary-length (but assumed finite) stream of lines, parses each line to a number, computes the Welford single-pass algorith
by nerdponx 4y ago
Command line tool that ingests an arbitrary-length (but assumed finite) stream of lines, parses each line to a number, computes the Welford single-pass algorithm for mean and standard deviation, and prints the output nicely to the console.
It's a good introduction to handling input and output, string manipulation, error handling, abstractions for iteration (or lack thereof), and language performance characteristics. All while being a fairly easy task that can be implemented easily but performantly in 60 lines of Python: https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/master/item/running-stats-cli/implementations/python/calc.py https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/ma...
It also lets you play around with different software architectures and abstraction models, introducing enough real-world complexity to prevent you from making too many assumptions, while the logic is simple enough to keep you from getting lost in deep algorithmic implementation.
I have started adding some of these implementations to a Git repo that I will eventually fill out with other "polyglot" comparisons. See here for (currently) Python, Nim, Perl, and (somewhat-overengineered) Common Lisp: https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/master/item/running-stats-cli https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/ma...
I have several more implementations that I haven't committed to Git yet, because I've been lazy.
I've learned a lot through this process, not just about individual programming languages, but also about "how things work" in the process of trying to figure out why Python is so much faster than every other language I've tried (on par with Nim, handily beating all others, haven't tried plain C yet).
- cb321 4y agoFWIW, I did a straightforward impl in Nim that was ~10X faster than the Python version in your repo.