环具有零执行时间 [英] Loop with a zero execution time

查看:221
本文介绍了环具有零执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有一个环具有零执行时间?我会认为,既然有一个与之关联的开销,即使是空的循环应该有一个执行时间。

Is it possible to have a loop which has a zero execution time? I would think that even an empty loop should have an execution time since there is an overhead associated with it.

推荐答案

是的,下的 AS-if规则的编译器只有义务效仿code的观察行为,所以如果你有一个循环,没有任何可观察到的行为,那么它完全可以被优化掉,因此将有效地具有零的执行时间。

Yes, under the as-if rule the compiler is only obligated to emulate the observable behavior of the code, so if you have a loop that does not have any observable behavior then it can be optimized away completely and therefore will effectively have zero execution time.

例子

例如下面的code:

int main()
{
  int j = 0 ;
  for( int i = 0; i < 10000; ++i )
  {
    ++j ;
  }
}

编译GCC 4.9

使用 -O3 标志基本上结束了减少以下( <一个href=\"http://gcc.godbolt.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%2C%22colouriseAsm%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22sourcez%22%3A%22JYOwLgBAtghqAUBKAUAb2RCpICsIF4IAGCAbgwgDMB7AJ3i3CwONOYB4IBGI3otgNQDgEFJnSZMQvOUwBfZHKAAA%22%2C%22compiler%22%3A%22%2Fopt%2Fgcc-4.9.0%2Fbin%2Fg%2B%2B%22%2C%22options%22%3A%22-std%3Dc%2B%2B11%20-O2%20-fverbose-asm%20-fno-inline-small-functions%20%22%7D%5D%7D\"相对=nofollow>看直播 的):

compiled with gcc 4.9 using the -O3 flag basically ends up reducing to the following (see it live):

main:
  xorl  %eax, %eax  #
  ret

pretty多所允许秋季所有优化的的 AS-if规则的,我知道的唯一的例外是的复制elison 它允许以实现可观察到的行为。

Pretty much all optimizations allowed fall under the as-if rule, the only exception I am aware of is copy elison which is allowed to effect the observable behavior.

一些其他示例包括死code消除可以删除code编译器可以证明将永远不会被执行。例如,即使下面的循环确实含有副作用,可以优化掉了,因为我们可以证明这一点永远不会被执行( 看直播 的):

Some other examples would include dead code elimination which can remove code that the compiler can prove will never be executed. For example even though the following loop does indeed contain a side effect it can be optimized out since we can prove it will never be executed (see it live):

#include <stdio.h>

int main()
{
  int j = 0 ;
  if( false ) // The loop will never execute
  {
    for( int i = 0; i < 10000; ++i )
    {
      printf( "%d\n", j ) ;
      ++j ;
    }
  }
}

循环将优化掉一样previous例子。一个更有趣的例子就是,在一个循环的计算可以推断为一个常数,从而避免需要一个循环(不知道这属于在什么优化类的),例如案例:

int j = 0 ;
for( int i = 0; i < 10000; ++i )
{
  ++j ;
}
printf( "%d\n", j ) ;

可以优化客场( 看直播 的):

movl    $10000, %esi    #,
movl    $.LC0, %edi #,
xorl    %eax, %eax  #
call    printf  #

我们可以看到有没有参与循环。

We can see there is no loop involved.

在哪里是,如果规则涵盖在标准

如果作为规则的覆盖在C99标准草案 5.1.2.3 程序执行的它说

The as-if rule is covered in the draft C99 standard section 5.1.2.3 Program execution which says:

在抽象机,由指定的所有前pressions进行评估
  的语义。实际上,在实现不评价的一个组成部分
  前pression如果它可以推断​​出它的值不使用,并且没有
  所需的副作用产生(包括任何引起调用
  功能或访问volatile对象)。

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

借助 AS-if规则也适用于C ++, GCC 将产生在C ++模式相同的结果也是如此。 C ++标准草案涉及本节 1.9 程序执行的:

The as-if rule also applies to C++, gcc will produce the same result in C++ mode as well. The C++ draft standard covers this in section 1.9 Program execution:

在本国际标准中的语义描述定义
  参数不确定的抽象机。这次国际
  标准对符合结构没有要求
  实现。具体地,它们不需要复制或模拟
  抽象机的结构。相反,符合实现
  被要求(只)模拟抽象的观察行为
  机器作为解释below.5

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.5

这篇关于环具有零执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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