Skip to content

Instantly share code, notes, and snippets.

@cararemixed
Created April 6, 2021 03:03
Show Gist options
  • Save cararemixed/c232b888b9dbdaeb67967f357a3605df to your computer and use it in GitHub Desktop.
Save cararemixed/c232b888b9dbdaeb67967f357a3605df to your computer and use it in GitHub Desktop.
useless microbenchmark

For ruby:

require 'benchmark'
puts Benchmark.measure {(1..100_000).reduce(&:*); nil}.real

I get 7.48 to 7.68 seconds as the runtime on ruby 2.6.3 and 7.71 to 8.08 for ruby 3.0.0.

For elixir:

defmodule Factorial do
  def run do
    Enum.reduce(1..100_000, 1, & &1 * &2)
    :ok
  end
end

micros = :timer.tc(Factorial, :run, []) |> elem(0)
IO.puts "#{micros / 1_000_000} seconds"

I get 4.68 to 4.71 seconds for Erlang/OTP 24 RC2 with Elixir 1.11.4.

@coolmenu
Copy link

coolmenu commented Apr 6, 2021

for ruby I get 3.9 sec
for elixir I get 4.891474 sec
in my apple m1 air (16g)

@cararemixed
Copy link
Author

@coolmenu I'm curious what build versions you're using and if you're using rosetta 2. I don't have an M1 Mac to try this with but I'd expect the erlang JIT to work quite well with it as it generates code during startup so translation caches can be effective. You might see it drop well below ruby's runtime. If you're using homebrew + rosetta you can install the JIT version with brew install --HEAD erlang.

For hardware info, I'm running this on an 8-core i9 with macOS 11.2.3.

@yasoob
Copy link

yasoob commented Sep 12, 2023

Ran both in the interactive shells. Here is what I got:

1.052038 seconds for Elixir (Erlang/OTP 26 [erts-14.0.2])
4.762765 seconds for Ruby (irb 1.6.2)

Machine is intel-based Macbook Pro 32GB 8 core i9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment