哪种方式更快?如果elseif或选择大小写 [英] Which way is faster? If elseif or select case

查看:199
本文介绍了哪种方式更快?如果elseif或选择大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码,

  If Sheets(sheet1)。Range(A1)。Value = option_1然后
表(sheet1)。范围(A1)。Value =option_2
ElseIf Sheets(sheet1)。Range(A1)。Value =option_2然后
Sheets(sheet1)。Range(A1)。Value =option_3
ElseIf Sheets(sheet1)。Range(A1)。Value =option_3Then
表(sheet1)。范围(A1)。值=option_4
...
结束IF

 选择案例表(sheet1)范围A1)值
案例Is =option_1
表格(sheet1)。范围(A1)Value =option_2
案例Is =option_2
表格(sheet1)范围(A1)Value =option_3
案例Is =option_3
表格(sheet1)。范围(A1)。 Value =option_4
...
结束选择

问题: / p>

1)我想知道哪种方式会更快。如果可能,可以解释技术细节?



2)无论效率如何,我应该使用哪种方法来实现更好的编码。



3)任何其他简单的方式来从数组中循环值?

解决方案


  1. case语句应该最小化处理器尝试更改其命令位置的次数。这样做会导致它浪费时钟周期,直到引用正确的命令。除非你正在写一些需要极度优化的内容,否则你不会注意到这个区别。

  2. 我倾向于case语句,因为它们更容易阅读。 (较少阅读=>更容易阅读)

  3. 如果这是您正在使用的确切数据,您可以将该值分割为_,并将最后一位数'mod'最大化价值可能。将字符串组合在一起以获得结果。


For the following code,

If Sheets("sheet1").Range("A1").Value = "option_1" Then
    Sheets("sheet1").Range("A1").Value = "option_2"
ElseIf Sheets("sheet1").Range("A1").Value = "option_2" Then
    Sheets("sheet1").Range("A1").Value = "option_3"
ElseIf Sheets("sheet1").Range("A1").Value = "option_3" Then
    Sheets("sheet1").Range("A1").Value = "option_4"
...
End IF

and

Select Case Sheets("sheet1").Range("A1").Value
    Case Is = "option_1"
        Sheets("sheet1").Range("A1").Value = "option_2"
    Case Is = "option_2"
        Sheets("sheet1").Range("A1").Value = "option_3"
    Case Is = "option_3"
        Sheets("sheet1").Range("A1").Value = "option_4"
    ...
End Select

Questions:

1) I am wondering which way would be faster. And if possible, tech detail could be explained?

2) Regardless the efficiency, which method should I use in this case, for the better coding.

3) Any other "simple" way to circle value from array?

解决方案

  1. Case statements are supposed to minimize the number of times the processor attempts to change its command location. Doing so will cause it to waste clock cycles until the correct commands are referenced. Unless you're writing something that needs to be extremely optimized you won't notice the difference.
  2. I lean towards case statements because they are easier to read. (less to read => easier to read)
  3. If this is the exact data you are using, you can split the value on '_' and increment the last digit 'mod' the highest value possible. Combine the strings back together to get your result.

这篇关于哪种方式更快?如果elseif或选择大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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