gpt4 book ai didi

ruby - Elixir 比 Ruby 慢吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:33 24 4
gpt4 key购买 nike

请帮我解决一个关于 Elixir 与 Ruby 性能的基准问题。

我尝试用两种语言实现相同的阶乘,Ruby 显示出比 Elixir 更好的结果:

# ruby_factorial_with_iterator.rb
def factorial_with_iterator(n)
res = 1
(1..n).each{|time| res *= time}
res
end

p "factorial_with_iterator(200000)"
p factorial_with_iterator(200000)

运行后:

$ time ruby ruby_factorial_with_iterator.rb
real 0m18.378s
user 0m17.348s
sys 0m0.844s

和两个 Elixir 示例:

# elixir_factorial_with_iterator.exs
defmodule FactorialWithIterator do
def of(n) do
Enum.reduce(1..n, 1, &*/2)
end
end

IO.puts "Factorial of 200000: "
IO.puts FactorialWithIterator.of(200000)

运行后:

$ time elixir elixir_factorial_with_iterator.exs
real 1m1.735s
user 1m1.556s
sys 0m0.104s

另一个例子:

# elixir_factorial_with_recursion.exs
defmodule FactorialWithRecursion do
def of(0), do: 1
def of(n) when n > 0 do
n * of(n - 1)
end
end

IO.puts "Factorial of 200000: "
IO.puts FactorialWithRecursion.of(200000)

运行后:

$ time elixir elixir_factorial_with_recursion.exs
real 1m7.149s
user 1m6.248s
sys 0m0.092s

为什么会有如此巨大的差异:Elixir - 1m1s,而 Ruby - 只有 18s?或者如何在 Elixir 中编写正确的迭代代码?

附言环境:

  • Ubuntu 16.04
  • ruby 2.3.1p112(2016-04-26 修订版 54768)[x86_64-linux]
  • Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
  • Elixir 1.4.4

最佳答案

正如其中一条评论中提到的,您正在使用 time,这也是启动 VM 的时间,在 elixir 的情况下,是将代码编译为 BEAM 字节码的时间。为避免计算所有这些,您应该使用语言本身的基准测试工具。

我很好奇,所以我尝试自己对这些函数进行基准测试。

我用过:

ruby :

require 'benchmark/ips'

def factorial_with_iterator(n)
res = 1
(1..n).each{|time| res *= time}
res
end

Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report('factorial_with_iterator.rb') do
factorial_with_iterator(200000)
end
x.compare!
end

Elixir :

defmodule Factorial do
def iter(n) do
Enum.reduce(1..n, 1, &*/2)
end

def recur(0), do: 1

def recur(n) when n > 0 do
n * recur(n - 1)
end
end

Benchee.run(%{
"factorial_with_iter.ex" => fn -> Factorial.iter(200000) end,
"factorial_with_recur.ex" => fn -> Factorial.recur(200000) end
})

我得到了这些结果:

ruby :

Warming up --------------------------------------
factorial_with_iterator.rb
1.000 i/100ms
Calculating -------------------------------------
factorial_with_iterator.rb
0.033 (± 0.0%) i/s - 1.000 in 29.994713s

Elixir :

Name                              ips        average  deviation         median         99th %
factorial_with_iter.ex 0.0395 25.29 s ±0.00% 25.29 s 25.29 s
factorial_with_recur.ex 0.0368 27.17 s ±0.00% 27.17 s 27.17 s

Comparison:
factorial_with_iter.ex 0.0395
factorial_with_recur.ex 0.0368 - 1.07x slower

因此,这些结果表明 Elixir 在两种实现方式下都稍快一些,Ruby 需要大约 30 秒,而 Elixir 需要大约 25 和 27 秒。

虽然使用“每秒迭代次数”,但对于需要比一秒长得多的函数来说可能有点“错误”。所以我也尝试了低得多的输入。我使用了 1_000 而不是 200_000,并得到了这些结果:

ruby :

Warming up --------------------------------------
factorial_with_iterator.rb
169.000 i/100ms
Calculating -------------------------------------
factorial_with_iterator.rb
1.750k (± 8.0%) i/s - 8.788k in 5.064619s

Elixir :

Name                              ips        average  deviation         median         99th %
factorial_with_recur.ex 3.15 K 317.36 μs ±12.72% 306 μs 481.87 μs
factorial_with_iter.ex 3.02 K 331.13 μs ±16.83% 316 μs 559 μs

Comparison:
factorial_with_recur.ex 3.15 K
factorial_with_iter.ex 3.02 K - 1.04x slower

奇怪的是,这表明 Elixir 比 Ruby 快得多。对于这两种实现,Elixir 每秒能够执行超过 3k 次迭代,而 Ruby 每秒只能执行 1.75k 次迭代。

使用:

  • ruby 2.4.1p111(2017-03-22 修订版 58053)[x86_64-darwin16]
  • Elixir 1.6.5(使用 OTP 19 编译)运行在 Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async -threads:1] [hipe] [dtrace]

关于ruby - Elixir 比 Ruby 慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com