什么可以优化? [英] what can be optimized?

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

问题描述

您好。


可以在C ++代码中优化什么以及如何保证稳定

以下行为


1.

表达式是否为auto volatile可以拒绝删除未使用的临时

,如下所示:

auto volatile const class_name tmp;


如果没有,我该怎么办呢?


2.

C ++可以保证,这个未命名的临时对象不会被删除

未使用的临时这里:


A类{A(){cout<<" start ..." ;; } $

B类{B(){cout<<" ok"<< endl; } $

void inp();

void test(){A(); INP(); B(); }

Hello.

What can be optimised in C++ code and how i can garantee stable
behaviour below

1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:

auto volatile const class_name tmp;

If not, how can i do it else?

2.
Can C++ garantee, that unnamed temporary object will not be removed as
"unused temporary" here:

class A{ A(){cout<<"start..."; } }
class B{ B(){cout<<"ok"<<endl; } }

void inp();
void test(){ A(); inp(); B(); }

推荐答案




1月24日上午11点50分,Grizlyk ; < grizl ... @ yandex.ruwrote:


On Jan 24, 11:50 am, "Grizlyk" <grizl...@yandex.ruwrote:

Hello。


可以在C ++代码中优化什么以及如何我可以保证稳定

行为低于
Hello.

What can be optimised in C++ code and how i can garantee stable
behaviour below



只要可见行为不受影响,任何事情都可能被优化

受影响。可见行为是程序输出的任何内容,但不是

,例如需要多长时间。

Anything might be optimised as long as visible behaviour is not
affected. Visible behaviour is anything output by the program, but not
e.g. how long time something takes.


>

1.

表达式是否为auto volatile可以拒绝删除未使用的临时

,如下所示:


auto volatile const class_name tmp;
>
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:

auto volatile const class_name tmp;



No.

No.


>

如果没有,我怎么能做其他事情?
>
If not, how can i do it else?



你有什么问题?优化不应该导致问题(除非

优化的代码是错误的,当然)。

What is your problem? Optimisation should not cause problems (unless
the optimised code is wrong, of course).


>

2.

可以C ++保证,未命名的临时对象不会被删除,因为

unused temporary这里:
>
2.
Can C++ garantee, that unnamed temporary object will not be removed as
"unused temporary" here:



是的。任何有副作用的东西通常都不会被优化

。此外,删除下面的临时对象会改变输出

这是可见的行为。

Yes. Anything that has a side-effect will normally not be optimised
away. Also, removal of your temporary objects below would change output
which is visible behaviour.


>

A类{A(){cout<<" start ..." ;; } $

B类{B(){cout<<" ok"<< endl; } $

void inp();

void test(){A(); INP(); B(); }
>
class A{ A(){cout<<"start..."; } }
class B{ B(){cout<<"ok"<<endl; } }

void inp();
void test(){ A(); inp(); B(); }



/ Peter

/Peter


" peter koch"写道:
"peter koch" wrote:


可以用C ++代码优化什么以及如何保证

低于
的稳定行为

What can be optimised in C++ code and how i can garantee
stable behaviour below



只要可见行为不受影响,任何事情都可能被优化。

Anything might be optimised as long as visible behaviour is not
affected.



我说过,编译器和程序员可以考虑可见

行为。不同。例如,编译器经常消除

无法使用代码如果没有使用编译器思考代码。

I said, that compiler and programmer can think about "visible
behaviour" differently. Compiler for example, often eliminate
"unusable" code if compiler think code is not used.


1.

表达式是否为auto volatile可以拒绝删除未使用的临时

,如下所示:
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:


auto volatile const class_name tmp; No。
auto volatile const class_name tmp;No.


你有什么问题?

What is your problem?



在临时对象编译器的情况下,可以将对象移到

点,其中将使用对象,例如:


{

int tmp = 0;


如果(!is_ready)返回tmp;

返回1;

}


可以优化为


{

if(!is_ready){int tmp = 0;返回tmp; }

返回1;

}


可以自动挥动强制对象放入堆栈,而不是放入CPU

寄存器?

In the case of temporary object compiler could remove object to the
point, where object will be used, for example:

{
int tmp=0;

if(!is_ready)return tmp;
return 1;
}

can be optimized to

{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}

Can "auto volatile" force object to be placed into stack, not into CPU
registers?


2.

C ++可以保证,未命名的临时对象不会被删除为未使用的临时对象。这里:
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:



如果是临时物品


A级;

B级;

void test(){A(); INP(); B(); } $ / $

编译器可以消除创建的对象,但之后永远不会使用




void test() {inp(); }


因为A();和B();从未使用过未命名的对象。

In the case of temporary object

class A;
class B;
void test(){ A(); inp(); B(); }

compiler could eliminate objects, that created but after that never
used:

void test(){ inp(); }

because A(); and B(); are never used unnamed objects.


Grizlyk写道:
Grizlyk wrote:

peter koch写道:
"peter koch" wrote:

>

可以在C ++代码中优化什么以及如何保证

稳定行为低于
>
What can be optimised in C++ code and how i can garantee
stable behaviour below


只要可见行为不受影响,任何事情都可能得到优化。

Anything might be optimised as long as visible behaviour is not
affected.



我说过,编译器和程序员可以考虑可见

行为。不同。例如,编译器经常消除

无法使用代码,如果没有使用编译器思考代码。


I said, that compiler and programmer can think about "visible
behaviour" differently. Compiler for example, often eliminate
"unusable" code if compiler think code is not used.



编译器不只是随机猜测什么是不需要的。它通常会删除实际上没有使用的代码,即使你可能认为它是b $ b。在低级编程中有一些特殊情况(直接与硬件,操作系统内核等进行交互)需要更多的控制。这就是那里的不稳定因素。

The compiler doesn''t just do random guesses about what isn''t needed. It
usually removes code that actually isn''t used, even if you might think it
is. There are some special circumstances in low-level programming (direct
interaction with hardware, operating system kernels and such) where more
control is needed. That''s what volatile is there for.


1.

表达式是自动波动吗?可以拒绝删除未使用的临时

,如下所示:
1.
Are expression "auto volatile" can deny removing as "unused temporary"
like this:


auto volatile const class_name tmp; No。
auto volatile const class_name tmp;No.


你有什么问题?

What is your problem?



在临时对象编译器的情况下,可以将对象移到

点,其中将使用对象,例如:


{

int tmp = 0;


如果(!is_ready)返回tmp;

返回1;

}


可以优化为


{

if(!is_ready){int tmp = 0;返回tmp; }

返回1;

}


In the case of temporary object compiler could remove object to the
point, where object will be used, for example:

{
int tmp=0;

if(!is_ready)return tmp;
return 1;
}

can be optimized to

{
if(!is_ready){ int tmp=0; return tmp; }
return 1;
}



它甚至可以优化它:


{

if(!is_ready){return 0; }

返回1;

}

或者甚至可以这样做:


{

返回is_ready!= 0;

}


但为什么会出现问题?

It can even optimize it to:

{
if(!is_ready){ return 0; }
return 1;
}
Or it might even do:

{
return is_ready != 0;
}

But why would that be a problem?


可以自动挥发强制对象被放入堆栈,而不是进入CPU

寄存器?
Can "auto volatile" force object to be placed into stack, not into CPU
registers?



在某些编译器上,确实如此,但我不确定这是否需要

标准。

On some compilers, it does, but I''m not sure if that''s required by the
standard.


2.

C ++可以保证,那个未命名的临时对象不会将

删除为未使用的临时这里:
2.
Can C++ garantee, that unnamed temporary object will not be
removed as "unused temporary" here:



如果是临时物品


A级;

B级;

void test(){A(); INP(); B(); } $ / $

编译器可以消除创建的对象,但之后永远不会使用




void test() {inp(); }


因为A();和B();永远不会使用未命名的对象。


In the case of temporary object

class A;
class B;
void test(){ A(); inp(); B(); }

compiler could eliminate objects, that created but after that never
used:

void test(){ inp(); }

because A(); and B(); are never used unnamed objects.



只有当他们的构造确保没有副作用时,编译器才可能优化这些对象,即如果你是无论如何,
都不会注意到它。

The compiler might optimize those objects away only if their construction
and destruction are guaranteed to have no side effects, i.e. if you
wouldn''t notice it anyway.


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

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