Ruby 基准违规来源

发布于 2024-12-19 06:35:20 字数 950 浏览 0 评论 0原文

运行此代码:

require 'benchmark'

Benchmark.bm do |x|
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
end

输出以下结果:

       user     system      total        real
1+1  2.188000   0.000000   2.188000 (  2.250000)
1+1  2.250000   0.000000   2.250000 (  2.265625)
1+1  2.234000   0.000000   2.234000 (  2.250000)
1+1  2.203000   0.000000   2.203000 (  2.250000)
1+1  2.266000   0.000000   2.266000 (  2.281250)

猜测变化是系统环境的结果,但想确认情况确实如此。

Running this code:

require 'benchmark'

Benchmark.bm do |x|
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
  x.report("1+1") {15_000_000.times {1+1}}
end

Outputs these results:

       user     system      total        real
1+1  2.188000   0.000000   2.188000 (  2.250000)
1+1  2.250000   0.000000   2.250000 (  2.265625)
1+1  2.234000   0.000000   2.234000 (  2.250000)
1+1  2.203000   0.000000   2.203000 (  2.250000)
1+1  2.266000   0.000000   2.266000 (  2.281250)

Guessing the variation is a result of the system environment, but wanted to confirm this is the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-12-26 06:35:21

“猜测变化是系统环境的结果”,你是对的。

基准不可能始终准确。你没有一台完美的常规机器来总是同时运行某些东西。如果两个数字太接近,则将基准中的两个数字视为相同,如本例所示。

"Guessing the variation is a result of the system environment", you are right.

Benchmarks can't be precise all time. You don't have a perfect regular machine to run something always in the same time. Take two numbers from benchmark as the same if they were too near, as in this case.

陌生 2024-12-26 06:35:21

我尝试使用 eval 部分展开循环,虽然它使速度更快,但它使执行时间不太一致!

$VERBOSE &&= false # You do not want 15 thousand "warning: useless use of + in void context" warnings
# large_number = 15_000_000 # Too large! Caused eval to take too long, so I gave up
somewhat_large_number = 15_000
unrolled = "def do_addition\n" + ("1+1\n" * somewhat_large_number) + "end\n" ; nil
eval(unrolled)

require 'benchmark'

Benchmark.bm do |x|
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
end

      user     system      total        real
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.765586)
1+1 partially unrolled  0.765000   0.000000   0.765000 (  0.765586)
1+1 partially unrolled  0.688000   0.000000   0.688000 (  0.703089)
1+1 partially unrolled  0.797000   0.000000   0.797000 (  0.796834)
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.749962)
1+1 partially unrolled  0.781000   0.000000   0.781000 (  0.781210)
1+1 partially unrolled  0.719000   0.000000   0.719000 (  0.718713)
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.749962)
1+1 partially unrolled  0.765000   0.000000   0.765000 (  0.765585)
1+1 partially unrolled  0.781000   0.000000   0.781000 (  0.781210)

为了比较的目的,您在我的计算机上的基准给出了

      user     system      total        real
1+1  2.406000   0.000000   2.406000 (  2.406497)
1+1  2.407000   0.000000   2.407000 (  2.484629)
1+1  2.500000   0.000000   2.500000 (  2.734655)
1+1  2.515000   0.000000   2.515000 (  2.765908)
1+1  2.703000   0.000000   2.703000 (  4.391075)

最后一行的实时变化,但不是用户或总计)

I tried using eval to partially unroll the loop, and although it made it faster, it made the execution time less consistent!

$VERBOSE &&= false # You do not want 15 thousand "warning: useless use of + in void context" warnings
# large_number = 15_000_000 # Too large! Caused eval to take too long, so I gave up
somewhat_large_number = 15_000
unrolled = "def do_addition\n" + ("1+1\n" * somewhat_large_number) + "end\n" ; nil
eval(unrolled)

require 'benchmark'

Benchmark.bm do |x|
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
  x.report("1+1 partially unrolled") { i = 0; while i < 1000; do_addition; i += 1; end}
end

gave me

      user     system      total        real
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.765586)
1+1 partially unrolled  0.765000   0.000000   0.765000 (  0.765586)
1+1 partially unrolled  0.688000   0.000000   0.688000 (  0.703089)
1+1 partially unrolled  0.797000   0.000000   0.797000 (  0.796834)
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.749962)
1+1 partially unrolled  0.781000   0.000000   0.781000 (  0.781210)
1+1 partially unrolled  0.719000   0.000000   0.719000 (  0.718713)
1+1 partially unrolled  0.750000   0.000000   0.750000 (  0.749962)
1+1 partially unrolled  0.765000   0.000000   0.765000 (  0.765585)
1+1 partially unrolled  0.781000   0.000000   0.781000 (  0.781210)

For the purpose of comparison, your benchmark on my computer gave

      user     system      total        real
1+1  2.406000   0.000000   2.406000 (  2.406497)
1+1  2.407000   0.000000   2.407000 (  2.484629)
1+1  2.500000   0.000000   2.500000 (  2.734655)
1+1  2.515000   0.000000   2.515000 (  2.765908)
1+1  2.703000   0.000000   2.703000 (  4.391075)

(real time varied in the last line, but not user or total)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文