Benford’s Law

After watching a Netflix documentary about Benford’s Law (“connected” Season 1 Episode 4 “Digits”), I got really interested in it because it can help detect fraud in e.g. balance sheets. So I searched my favorite programming languages and detected that there is no npm Package for checking Benford’s Law.

Sooo … Long story short: I created nearly identical libraries for doing a Benford’s Law check in JavaScript, Go and Crystal. My inspiration was from a Python library - so the algorithm of these libraries is nearly identical. There are some catches like I used many different math libraries for calculating chi-square and some of these libraries offer addition information, others not …

But I think this gives us a good example of a algorithm which is implemented in different programming languages and can be benchmarked. That’s what I wanna do in this post: Comparing the Benford’s Law check in different Languages.

Testing the Performance

Repo with Source Code:

Language & Version GitHub Repo of Library Performance
Python 3.8.5 huntlib 1,86 secs
Crystal 0.35.1 14,20 millis
Go 1.15 benford 7,03 millis
JS (node) v14.8.0 benfordslaw 164,71 millis

I didn’t expected that big difference. Go is clearly the winner here and that with big distance to the other implementations. One reason why Python took so long may be that the used library shows additional information but I don’t think Python could compete with the other implementations if the library wouldn’t show more.

It is also noteworthy that Go is very very fast compiling. Crystal isn’t even in the area of the Go compile time.