有效的分配??? [英] efficient assignment ???

查看:73
本文介绍了有效的分配???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是:


int x;

for(int count = 0; count< 200; count ++)

{

x = someFunc();

anotherFunc(x);

}

比这更有效:< ($ count = 0; count< 200; count ++)

{

int x = someFunc();

/>
anotherFunc(x);

}


有人也可以解释原因吗?

谢谢。程序员写道:


这是:

int x;
for(int count = 0; count< 200; count ++)
{
x = someFunc();
anotherFunc(x);
}

比这更有效:

for(int count = 0; count< 200; count ++)
{
int x = someFunc();
anotherFunc( x);
}

有人也可以解释原因吗?
谢谢。




用你的compil试试吧呃。您可以使用时间

函数clock()来计算这一个


例如。


#include < cstdio>

#include< cstdlib>

#include< ctime>


using namespace std;


int main()

{

clock_t start = clock();


//你的代码在这里


clock_t finish = clock();


double duration =(double)(finish - start)/ CLOCKS_PER_SEC ;

printf("%2.f seconds \ n",duration);

}


但要确保,持续时间足够长。通常只需要重新定价

你需要花费几百万倍的时间就足够长了,比如说钟表()的调整再也不重要了(In换句话说:

确保你的程序运行至少几秒钟。

-

Karl Heinz Buchegger
kb ****** @ gascad.at





程序员写道:

这是:

int x;
for(int count = 0; count< 200; count ++)
{
x = someFunc();
anotherFunc(x);
}

比这更有效:

for(int count = 0; count< 200; count ++)
{x /> int x = someFunc();
anotherFunc(x);
} <有人也可以解释原因吗?
谢谢。




您可能会发现,对于POD,编译器会优化示例2

与示例1相似。对于非POD

变量,这不一样。我已经看到代码看起来像下降了17%:


for(int count = 0; count< 200; count ++)

{

myClass a = someFunc();

anotherFunc(a);

}


运行探查器。


>这是:


int x;
for(int count = 0; count< 200; count ++)
{
x = someFunc();
anotherFunc(x);
}

比这更有效:

for(int count = 0; count< 200; count ++)
{
int x = someFunc();
anotherFunc(x);
}




将优化第二种格式由你的编译器到第一个。

我会说第二个运行速度与第一个一样快,因为声明不需要b $ b需要任何处理。 />

问候,

巴特


Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.

解决方案

Programmer wrote:


Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}

more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.



Try it with your compiler. You can use the timing
functions clock() to figure this one out

eg.

#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
clock_t start = clock();

// your code goes here

clock_t finish = clock();

double duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.f seconds\n", duration );
}

But make sure, that the duration is long enough. Usually just reapeating
what you need to time a few million times makes it long enough, such
that the resotion of clock() doesn''t matter any more (In other words:
make sure your program runs at least a few seconds).
--
Karl Heinz Buchegger
kb******@gascad.at




Programmer wrote:

Is this:

int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}

can someone also explain why?
thanks.



You''ll probably find that for PODs the compiler will optimize example 2
to be similar to example 1. This won''t be the same for non POD
variables. I''ve seen as much as 17% slowdown for code that looked like:

for (int count=0; count<200; count++)
{
myClass a = someFunc();
anotherFunc(a);
}

run the profiler.


> Is this:


int x;
for (int count=0; count<200; count++)
{
x = someFunc();
anotherFunc(x);
}
more efficient than this:

for (int count=0; count<200; count++)
{
int x = someFunc();
anotherFunc(x);
}



The second format will be optimized by your compiler to the first one.
I''d say the second runs as fast as the first, since declarations don''t
need any processing.

regards,
Bart


这篇关于有效的分配???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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