良好做法:循环,如果声明 [英] Good Practice: Loop And If statement

查看:135
本文介绍了良好做法:循环,如果声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我复制/粘贴同样的问题,我在代码审查张贴在这里:的 http://codereview.stackexchange.com/questions/1747/good-practice-loop-and-if-statement

我想知道什么是最好的做法:

I'd like to know what is the best practice:

A版本:

loop1
  if condition1
    code1

  if condition2
    code2

  if condition3
    code3

或者,b版:

if condition1
  loop1 with code1

if condition2
  loop1 with code2

if condition3
  loop1 with code3

我实现了版本b的原因,而是我更可读,时时检查相同的条件似乎很荒谬

I've implemented version B cause it's more readable for me, and always checking the same condition seems absurd.

但循环n次在同一阵列可以被看作是荒唐,)

But looping n times the same array could be seen as absurd too :)

推荐答案

从你正在考虑的版本B,它具有循环外的条件,事实上,我认为条件的真值不与数组中的元素而有所不同。否则,您的问题将没有意义。

From the fact that you are considering version B, which has the conditions outside of the loops, I assume that the the truth value of the conditions do not vary with the elements within the array. Otherwise, your question will not make sense.

结论

如果条件很复杂,使他们花费很多时间在相对评价到阵列的大小,则版本B是更快。如果是其他方式,则A版本速度更快。

If the conditions are complicated so that they take much time in evaluation relative to the size of the array, then version B is faster. If it is the other way around, then version A is faster.

和这是更快应该在这种情况下,一致与策略,你应该接受,因为它更容易被理解为嵌入一​​个复杂的结构内的一个简单的结构,而不是具有简单结构内的复杂结构的嵌入

And which is faster should coincide in this case with which strategy you should take because it is more comprehensible to have a simple structure embedded within a complicated structure rather than having an embedding of a complicated structure within a simple structure.

<强>解释

当的条件是相对于所述阵列的大小简单,迭代的成本重于评估条件的成本。如下面红宝石观察,版本B是比较慢。

When the condition is simple enough relative to the size of the array, the cost of iteration overweighs the cost of evaluating the condition. As observed below with ruby, version B is slower.

$a = (1..10000)

def versionA
  $a.each do
    nil if true
    nil if false
    nil if true
  end
end

def versionB
  $a.each {nil} if true
  $a.each {nil} if false
  $a.each {nil} if true
end

require 'benchmark'
n = 10000
Benchmark.bmbm do|b|
  b.report('A'){n.times{versionA}}
  b.report('B'){n.times{versionB}}
end

Rehearsal -------------------------------------
A   7.270000   0.010000   7.280000 (  7.277896)
B  13.510000   0.010000  13.520000 ( 13.515172)
--------------------------- total: 20.800000sec

        user     system      total        real
A   7.200000   0.020000   7.220000 (  7.219590)
B  13.580000   0.000000  13.580000 ( 13.605983)

在另一方面,如果评估的条件是相对于更昂贵的迭代过阵列,则前者将变得比后者更重要的作用,其速度将是其他方式。

On the other hand, if evaluating the conditions is more costly relative to iteration over the array, then the effect of the former will become more crucial than the latter, and the speed will be the other way around.

$a = (1..100)

def versionA
  $a.each do
    nil if (1..10).each{nil} && true
    nil if (1..10).each{nil} && false
    nil if (1..10).each{nil} && true
  end
end

def versionB
  $a.each {nil} if (1..10).each{nil} && true
  $a.each {nil} if (1..10).each{nil} && false
  $a.each {nil} if (1..10).each{nil} && true
end

require 'benchmark'
n = 10000
Benchmark.bmbm do|b|
  b.report('A'){n.times{versionA}}
  b.report('B'){n.times{versionB}}
end

Rehearsal -------------------------------------
A   2.860000   0.000000   2.860000 (  2.862344)
B   0.160000   0.000000   0.160000 (  0.169304)
---------------------------- total: 3.020000sec

        user     system      total        real
A   2.830000   0.000000   2.830000 (  2.826170)
B   0.170000   0.000000   0.170000 (  0.168738)

这篇关于良好做法:循环,如果声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆