Casting会减慢程序的速度吗? [英] Does Casting Slow a Program Down?

查看:56
本文介绍了Casting会减慢程序的速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的代码。


unsigned short usLimit = 10

int a [10],i;


for(i = 0; i<(int)usLimit; ++ i)

{

a [i] =(int)usLimit;

}


它会比这慢吗?


int a [10],i,iLimit = 10;


for(i = 0; i< iLimit; ++ i)

{

a [i] = iLimit; < br $>
}


谢谢,

彼得。

If I had code like this.

unsigned short usLimit=10
int a[10], i;

for (i=0; i<(int)usLimit; ++i)
{
a[i]=(int)usLimit;
}

would it run slower than this?

int a[10], i, iLimit=10;

for (i=0; i<iLimit; ++i)
{
a[i]=iLimit;
}

Thanks,
Peter.

推荐答案

2007年1月31日06:50:47 -0800,PeterOut < Ma ********** @ excite.com>

写道:
On 31 Jan 2007 06:50:47 -0800, "PeterOut" <Ma**********@excite.com>
wrote:

>如果我有代码像这样。

unsigned short usLimit = 10
>If I had code like this.

unsigned short usLimit=10



<<<<<缺少分号

<<<<< Missing semicolon


> int a [10],i;

for(i = 0; i<(int)usLimit; + + i)

a [i] =(int)usLimit;
}
它会比这慢吗?

int a [10],i,iLimit = 10;

for(i = 0; i< iLimit; ++ i)
{

a [i] = iLimit;
}
>int a[10], i;

for (i=0; i<(int)usLimit; ++i)
{
a[i]=(int)usLimit;
}

would it run slower than this?

int a[10], i, iLimit=10;

for (i=0; i<iLimit; ++i)
{
a[i]=iLimit;
}



这取决于编译器,但答案通常应该是''不''


如果编译器检测到usLimit和iLimit是常量,那么它可以检测到(int)usLimit也是常量,它可能会产生
生成两个样本的完全相同的机器代码。


但这种行为取决于编译器的优化功能以及代码片段周围的代码你已经向我们展示过了。


你可以帮助编译器声明

const unsigned short usLimit = 10;



const int iLimit = 10;

祝你好运,

Zara

It depends on the compiler, but the answer should usually be ''No''

If the compiler detectes tha usLimit and iLimit are constants, then it
may detect that (int)usLimit is also constant and it would probably
generate the exact same machine code for both samples.

But this behaviour depends on the optimizations capabilities an
settings of the compiler, and of the code surrounding the snippet you
have shown us.

You may help the compiler declaring either
const unsigned short usLimit=10;
or
const int iLimit=10;
Best regards,

Zara


PeterOut说:
PeterOut said:

如果我有这样的代码。


unsigned short usLimit = 10

int a [10 ],i;


for(i = 0;我≤(INT)usLimit; ++ i)

{

a [i] =(int)usLimit;

}


它会比这慢吗?


int a [10],i,iLimit = 10;


for(i = 0; i< iLimit; ++ i)

{

a [i] = iLimit;

}
If I had code like this.

unsigned short usLimit=10
int a[10], i;

for (i=0; i<(int)usLimit; ++i)
{
a[i]=(int)usLimit;
}

would it run slower than this?

int a[10], i, iLimit=10;

for (i=0; i<iLimit; ++i)
{
a[i]=iLimit;
}



要查找,编写两个版本的代码,将它们放在单独的函数中,并将它们分别调用

。然后检查分析器的输出。


重复每个相关的实现。 (不要指望A在实现Y上比B更快,因为它在实现X上更快。)


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

To find out, code both versions, put them in separate functions, and call
them each a zillion times. Then check the profiler''s output.

Repeat on each relevant implementation. (Don''t expect A to be faster than B
on implementation Y just because it was faster on implementation X.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


PeterOut写道:
PeterOut wrote:

如果我有这样的代码。


unsigned short usLimit = 10

int a [10],i;


for(i = 0; i<(int)usLimit; ++ i)

{

a [i] =(int) usLimit;

}


它会比这慢吗?


int a [10],我, iLimit = 10;


for(i = 0; i< iLimit; ++ i)

{

a [i] = iLimit;

}
If I had code like this.

unsigned short usLimit=10
int a[10], i;

for (i=0; i<(int)usLimit; ++i)
{
a[i]=(int)usLimit;
}

would it run slower than this?

int a[10], i, iLimit=10;

for (i=0; i<iLimit; ++i)
{
a[i]=iLimit;
}



它可能,但它与演员阵容无关,

因为它们都是不必要的 - usLimit是

无论如何从`unsigned short`转换为`int`。


无论如何,标准对这个问题保持沉默。


-

Chris" electric hedg ehog" Dollin

过去的好日子过去好多了。

It might, but it would have nothing to do with the casts,
since they''re both unnecessary -- `usLimit` is
converted from `unsigned short` to `int` regardless.

In any case, the Standard is silent on the issue.

--
Chris "electric hedgehog" Dollin
The "good old days" used to be much better.


这篇关于Casting会减慢程序的速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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