我有三个条件要比较.哪两个在以下两个之间更快?请指出我.谢谢大家!
If var = 1 then
Command for updating database
ElseIf var = 2 then
Command for updating database
ElseIf var = 3 then
Command for updating database
EndIf
Run Code Online (Sandbox Code Playgroud)
和
Select Case var
Case 1
Command for updating database
Case 2
Command for updating database
Case 3
Command for updating database
End Select
Run Code Online (Sandbox Code Playgroud) 在PHP中,我想这样做:
switch (function_foo($bar,$bar2)) {
case $fu:
*Do Stuff*
break;
case $fubar:
*Do Other Stuff*
break;
}
Run Code Online (Sandbox Code Playgroud)
这是一个糟糕的主意吗?它会起作用吗?
开发Android时,switch语句比if-else链更有效吗?switch语句需要更多行代码,但查看轶事证据似乎是Android应用程序中更常用的.
下面的示例说明了具有case语句和if-else链的相同编程结构.switch语句需要10行,而if-else链需要7行.
案例陈述
public void onClickWithSwitch(View v) {
switch(v.getId()) {
case R.id.buttonA:
buttonA();
break;
case R.id.buttonB:
buttonB();
break;
case R.id.buttonC:
buttonC();
}
}
Run Code Online (Sandbox Code Playgroud)
如果 - 其他链
public void onClickWithIf(View v) {
int id = v.getId();
if(id == R.id.buttonA)
buttonA();
else if (id == R.id.buttonB)
buttonB();
else if (id == R.id.buttonC)
buttonC();
}
Run Code Online (Sandbox Code Playgroud)
为什么切换比if-else链更常见?与if-else链相比,switch语句提供更好的性能吗?
<? switch($data['type']) : ?>
<? case 'log': ?>
<? while ($row = $data['loop']->fetch()) : ?>
<table class="t-errors">
<tr>
<td>
<b>IP:</b> <? echo $row['LogShellIP']; ?>
<b>Command:</b> <? echo $row['LogShellCommand']; ?>
<b>Executed:</b> <? echo $row['LogShellReturn']; ?>
<b>Time:</b> <? echo format::time($row['LogShellTime']); ?>
</td>
</tr>
</table>
<? endwhile; ?>
<? break; ?>
<? case 'fatal': ?>
<? case 'warning': ?>
<? case 'notice': ?>
<? case 'unknown': ?>
<? while ($row = $data['loop']->fetch()) : ?>
<table class="t-errors">
<tr>
<td <? if ($row['LogErrorSeen'] == 0) …Run Code Online (Sandbox Code Playgroud) 我在Visual C中有一个大的switch语句,大约250个案例:
#define BOP -42
#define COP -823
#define MOP -5759
int getScarFieldValue(int id, int ivIndex, int rayIndex, int scarIndex, int reamIndex)
{
int returnValue = INT_MAX;
switch (id)
{
case BOP : returnValue = Scar[ivIndex][rayIndex].bop[scarIndex][reamIndex]; break;
case COP : returnValue = Scar[ivIndex][rayIndex].cop[scarIndex][reamIndex]; break;
case MOP : returnValue = Scar[ivIndex][rayIndex].mop[scarIndex][reamIndex]; break;
.....
default: return(INT_MAX);
}
}
Run Code Online (Sandbox Code Playgroud)
你会注意到#defines有一个很大的范围,从-1到-10,000.事情是狗慢,我想知道花几个小时重新定义这250个定义到更窄(甚至连续)的范围可以加快速度.我一直认为编译器会以一种使其数值无关的方式处理案例值,但我无法找到任何讨论来验证/使该假设无效.
switch ("B")
{
case "A":
break;
case "B":
continue;
case "C":
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
在C++中简单正确的代码,但是当在javascript中使用稳定的chrome时,它只会抛出一个错误"非法继续声明",看起来像在javascript中切换时不允许继续语句...听说返回但它只是返回并且不会继续. ..那么有没有办法继续在js切换?
我有一个协议
protocol P { }
Run Code Online (Sandbox Code Playgroud)
它由枚举实现
enum E: P {
case a
case b
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
我希望能够接收实例P,并返回一个特定值,如果它是E(将来会有其他枚举/结构等实现P).
我试过这个:
extension P {
var value: String {
switch self {
case E.a: return "This is an E.a"
default: return "meh"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这不编译
error: Temp.playground:14:16: error: enum case 'a' is not a member of type 'Self'
case E.a: return "hello"
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
case is E.a: return "This is an E.a"
Run Code Online (Sandbox Code Playgroud)
这只是给出了这个错误:
error: Temp.playground:14:19: error: enum element 'a' is …Run Code Online (Sandbox Code Playgroud) 让我们从一些代码开始吧.这是我程序的极简化版本.
#include <stdint.h>
volatile uint16_t dummyColorRecepient;
void updateColor(const uint8_t iteration)
{
uint16_t colorData;
switch(iteration)
{
case 0:
colorData = 123;
break;
case 1:
colorData = 234;
break;
case 2:
colorData = 345;
break;
}
dummyColorRecepient = colorData;
}
// dummy main function
int main()
{
uint8_t iteration = 0;
while (true)
{
updateColor(iteration);
if (++iteration == 3)
iteration = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
该程序编译警告:
./test.cpp: In function ‘void updateColor(uint8_t)’:
./test.cpp:20:25: warning: ‘colorData’ may be used uninitialized in this function [-Wmaybe-uninitialized]
dummyColorRecepient …Run Code Online (Sandbox Code Playgroud) 我对Haskell的解析规则感到困惑。
这很漂亮:
n = 5
m = 6
b = case (n, m) of
(5, 6) -> True
_ -> False
main = print b
Run Code Online (Sandbox Code Playgroud)
让我们把它复杂化得很小,让我们let在混合物中添加一个:
b =
let res = case (n, m) of
(5, 6) -> True
_ -> False
in not res
Run Code Online (Sandbox Code Playgroud)
(注意,为了简便起见,我省略的定义n,m并main从现在开始,他们进行相同的,我只是改变b)
糟糕,这里的问题:
wtf.hs:5:5: error: parse error on input ‘(’
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
我不知道,也许那是我没有得到的某种奇怪的缩进规则。好的,让我们用括号括起来:
b =
let res = case (n, m) of {
(5, …Run Code Online (Sandbox Code Playgroud) 今天,我正在用Java创建一个Tetris克隆,当需要实现块生成机制时,我编写了这个带有枚举的switch语句。我被告知要尽可能避免使用switch语句,但是我不确定在这里是否可以避免使用switch语句,除非我彻底修改了我最初基于继承的设计选择。
为了实现不同类型的块,我决定制作一个名为Block的抽象超类,并为每种特定类型创建一个子类。我还做了一个枚举来标记块的类型。例如,一个ZBlock扩展Block对象将为它的Type字段变量分配Type.ZBlock枚举。
private void spawnBlock(Type type){
switch(type){
case I:
currentBlock = new IBlock();
break;
case L:
currentBlock = new LBlock();
break;
case J:
currentBlock = new JBlock();
break;
case Z:
currentBlock = new ZBlock();
break;
case S:
currentBlock = new SBlock();
break;
case T:
currentBlock = new TBlock();
break;
default:
currentBlock = new OBlock();
}
}
Run Code Online (Sandbox Code Playgroud)
当我开始考虑不使用switch语句的方法时,除了摆脱所有子类并在单个非抽象Block类中对不同块的行为进行编程之外,我别无选择。但是,由于不同的块具有不同的形状和壁球数据,可能会变得一团糟。那么在这里switch语句是一个不错的选择吗?如果没有,如何改善我的代码,为什么?
switch-statement ×10
if-statement ×2
java ×2
performance ×2
php ×2
android ×1
c ×1
c++ ×1
case ×1
database ×1
g++ ×1
haskell ×1
html ×1
javascript ×1
let ×1
loops ×1
oop ×1
swift ×1
vb.net ×1
windows ×1