NimbleParsec - a simple and fast parser combinator for Elixir

I have written a small library to make such tests as scientific as possible and as easy as possible. It’s called schism, and it’s available on Github here (no hex package yet): GitHub - tmbb/schism: A library that makes it easy to have alternative implementation of functions for benchmarks

There is a post discussing it here in the forum: Schism - a library to make benchmarking easy (and heretical!)

Basically, it uses some macros to define alternative versions of code fragments (function definitions, parts of function definitions, etc). It then provides some utilities to make it easier to benchmark the changes with Benchee.

You use it like this:

defmodule YourModule do
  # Some code, don't need to touch here
  # The code here is the same for both versions
  schism "inline vs parsec" do
    dogma "parsec" do
      # single combinator, coded in such a way that it uses parsec
    end

    heresy "inline" do
      # same combinator, but avoiding parsec
    end
  end
  # some more code..
  # The code here is the same for both versions
end

With benchee you can then compare the dogma and the heresy and determine objectively what is faster. This is obviously something you can do yourself, but the beauty of it is that it provides a very simple API with almost no friction to test the impact of small changes in single functions on the performance of the whole application, without having to maintain two copies of the application.

EDIT: are you afraid the two implementations of the function are not compatible? Don’t worry, the defsnippet macro in the Schism package handles that situation pretty well. For what it’s worth, Schism was originally written to benchmark alternative implementations of nimble_parsec parsers.

4 Likes