请问C / C ++提供的最小的执行时间任何保证? [英] Does C/C++ offer any guarantee on minimal execution time?

查看:94
本文介绍了请问C / C ++提供的最小的执行时间任何保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编译器似乎是朝那个什么也不做,不排除他们环路礼貌?

Why do compilers seems to be polite toward loops that do nothing and do not eliminate them?

请问C标准要求的循环需要一定的时间?

Does the C standard require loops to take some time?

例如,下面的code:

Example, the following code:

void foo(void) {
    while(1) {
        for(int k = 0; k < 1000000000; ++k);
        printf("Foo\n");
    }
}

运行速度比这个慢:

runs slower than this one:

void foo(void) {
    while(1) {
        for(int k = 0; k < 1000; ++k);
        printf("Foo\n");
    }
}

甚至 -O3 优化级别。
我希望删除允许空循环,从而获得相同的速度在两个codeS。

even with -O3 optimization level. I would expect removing empty loops allowed and thus get the same speed on both codes.

时的花时间的副作用,应该是由编译器pserved $ P $?

Is "time spent" a side effect that should be preserved by a compiler?

推荐答案

没有,花不计入观察到的行为时由AS-if规则来保护:

No, time spent does not count as observable behaviour to be protected by the as-if rule:

[C ++ 14:1.8 / 5]: 一致性实现执行一个结构完整的计划应产生相同的观察行为的可能之一抽象机使用相同的程序和相同的输入的相应实例的执行。但是,如果任何这样执行包含未定义操作,该国际标准的地方不执行该程序与输入的执行要求(不即使考虑到操作preceding第一个未定义操作)。

[C++14: 1.8/5]: A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

[C ++ 14:1.5 / 8]:对符合标准的实现最低要求是:

[C++14: 1.5/8]: The least requirements on a conforming implementation are:


      
  • 到易失性对象的访问是根据抽象机的规则严格评价。

  •   
  • 在程序终止,写入到文件中的所有数据将是相同的,以根据所述抽象语义,该程序的执行将所产生的可能结果。

  •   交互式设备的
  • 的输入和输出的动态应发生在这样一个方式即促使输出一个程序等待输入之前被实际递送。什么构成一个互动装置是实现定义的。

  •   

这些以下统称为的观察到的行为的程序的 [注:的抽象与实际语义之间更严格的对应可能每个实现定义。的末端注意事项】

These collectively are referred to as the observable behavior of the program. [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. —end note ]

这些循环可以合法优化掉了,也的确有其标准的品牌的有意的试图使这样做更容易的情景:

Those loops can be legally optimised out and, indeed, there are scenarios in which the standard makes deliberate attempts to make doing so even easier:

[C ++ 14:1.10 / 24]:的实施可能会认为任何线程最终将执行下列操作之一:

[C++14: 1.10/24]: The implementation may assume that any thread will eventually do one of the following:


      
  • 终止

  •   
  • 请打电话给库I / O功能,

  •   
  • 访问或修改volatile对象,或

  •   
  • 执行同步操作或一个原子操作。

  •   

[注:的这是为了让编译器转换,如去除空循环,即使不能终止证明。的末端注意事项】

[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. —end note ]

您的编译器可能实际上是注意到这个循环中的这些的节目的意图似乎是减缓重复的文本输出的发射为礼貌。 :)

Your compiler may in fact be being "polite" in noticing that the intent of the loop in these programs appears to be in slowing down the emission of repeated text output. :)

这篇关于请问C / C ++提供的最小的执行时间任何保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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