检查变量对两个值的最快方法 [英] Fastest way to check variable against two values

查看:54
本文介绍了检查变量对两个值的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,我可以写:


a = 1


如果在[1,2]中:

做点什么...

c(以及许多其他语言):


int a = 1;


if(a == 1 || a == 2){

做点什么...

}


是否有更简洁,更清晰的语法?

解决方案

5月2日,8:06 * am,wswilson< wswil ... @ gmail .comwrote:


在python中,我可以写:


a = 1


如果在[1,2]中:

* *做某事......


在c(和许多其他语言)中:


int a = 1;


if(a == 1 || a == 2){

* *做点什么...


}


是否有更简洁,更清晰的语法?



这取决于很多东西,例如你使用的是什么类型的变量

(int,double,strings等)


在某些情况下,使用switch语句在这里很有用:


switch(a){

case 1 :case2:

/ *做点什么* /

休息;

案例3:案例4:

/ *做别的事情* /

休息;

默认:

/ *等* /

休息;

}

以上可能是使用跳转表来实现的,

这可能比嵌套if'更快。但是它只有在比较常数时才有效。

-

Fred Kleinschmidt



" wswilson" < ws ****** @ gmail.com写信息

新闻:06 ************************ ********** @ e39g2000 hsf.googlegroups.com ...


在python中,我写道:


a = 1


如果在[1,2]中:

做某事......



这应该是/ fast /方法的一个例子吗?我认为Python创建了一个包含1和2的数组,然后在其中搜索一个。


整洁而短暂并不一定快。无论你用C语言,它都会更快。


- Bartc

" Bartc" < bc@freeuk.comwrites:


" wswilson" < ws ****** @ gmail.com写信息

新闻:06 ************************ ********** @ e39g2000 hsf.googlegroups.com ...


>在python中,我写道:
<如果a [1,2]:
做某事......



这是这个吗?应该是/ fast /方法的一个例子?我认为Python创建了一个包含1和2的数组,然后搜索它。



他没有提到快速。他提到了更短的清洁语法。并且

它是。


>

整洁而短暂并不一定快。无论你用C做它,它都会更快。


In python, I could write:

a = 1

if a in [1, 2]:
do something...
In c (and many other languages):

int a = 1;

if (a == 1 || a == 2) {
do something...
}

Is there a shorter, cleaner syntax?

解决方案

On May 2, 8:06*am, wswilson <wswil...@gmail.comwrote:

In python, I could write:

a = 1

if a in [1, 2]:
* *do something...

In c (and many other languages):

int a = 1;

if (a == 1 || a == 2) {
* *do something...

}

Is there a shorter, cleaner syntax?

It depends on many things, such as what types of variables
you are using (int, double, strings, etc.)

In some cases, using a switch statement is useful here:

switch(a) {
case 1: case2:
/*do something */
break;
case 3: case 4:
/* do something else */
break;
default:
/* etc. */
break;
}
The above is likely to be implemented using a jump table,
which is probably faster than the nested if''s. But it
only works if the comparisons are to constants.
--
Fred Kleinschmidt



"wswilson" <ws******@gmail.comwrote in message
news:06**********************************@e39g2000 hsf.googlegroups.com...

In python, I could write:

a = 1

if a in [1, 2]:
do something...

Is this supposed to be an example of a /fast/ method? I think Python creates
an array containing 1 and 2, then searches it for a.

Being neat and short isn''t necessarily fast. However you do it in C, it will
be faster.

-- Bartc


"Bartc" <bc@freeuk.comwrites:

"wswilson" <ws******@gmail.comwrote in message
news:06**********************************@e39g2000 hsf.googlegroups.com...

>In python, I could write:

a = 1

if a in [1, 2]:
do something...


Is this supposed to be an example of a /fast/ method? I think Python creates
an array containing 1 and 2, then searches it for a.

Nowhere did he mention "fast". He mentioned shorter cleaner syntax. And
it is.

>
Being neat and short isn''t necessarily fast. However you do it in C, it will
be faster.


这篇关于检查变量对两个值的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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