Organizational Research By

Surprising Reserch Topic

why is elixir slowest among ruby and golang in solving project euler 5 using -'performance,elixir,numerical-computing'

why is elixir slowest among ruby and golang in solving project euler 5  using -'performance,elixir,numerical-computing'

I'm working on Project Euler problems in multiple languages just to see how productive and how fast languages are. In problem #5, we are asked to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20.

I implemented the solution in multiple languages. Here are the stats:

Golang 1.4.2 : ~0.58s
Ruby 2.2 MRI : ~6.7s
Elixir 1.0.5 : ~57s

Why is Elixir's performance so slow? I tried using the same optimizations in all languages. Caveat: I'm a FP and Elixir newbie.

Is there anything I can do to improve the performance in Elixir? If you used any profiling tools in figuring out a better solution, could you please include them in the response?

In Golang:

func problem005() int {
  i := 20
  for {
    for j := 20; j > 0; j-- {
      if i%j != 0 {
        i = i + 20
        continue outer
    return i
  panic("Should have found a solution by now")

In Ruby:

def self.problem005
  divisors = (1..20).to_a.reverse

  number = 20 # we iterate over multiples of 20

  until divisors.all? { |divisor| number % divisor == 0 } do
    number += 20

  return number

In Elixir:

def problem005 do
  divisible_all? = fn num ->
    Enum.all?((20..2), &(rem(num, &1) == 0))

  Stream.iterate(20, &(&1 + 20))
  |> Stream.filter(divisible_all?)
  |> Enum.fetch! 0


asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.