as-if

Timing vs the “as-if” rule

只谈情不闲聊 提交于 2021-02-07 17:54:28
问题 There is a great question about the "as-if" rule in general, but I wonder if there are any excpetions when it comes to measuring time. Consider this (taken from here slightly modified): using std::chrono; auto begin = steady_clock::now(); auto result = some_lengthy_calculation(some_params); auto end= std::chrono::steady_clock::now(); std::cout << "Time diff = " << duration_cast<microseconds>(end - begin).count() <<std::endl; std::cout << "Result = " << result; The compiler is allowed to apply

Where are the statement of or the foundations for the “as if” rule in the C++ Standard?

試著忘記壹切 提交于 2020-05-15 02:42:04
问题 After a little google search (for instance, site:eel.is "as if rule" ) I couldn't find a proper place where the so called "as if" rule is clearly stated in the C++ standard. All I could find is that in those places within the standard where it is invoked, the intro.execution reference is given. But intro.execution doen't seem to clearly reference any general form of this rule. I'm probably missing something subtle here, but can you point me to the place, or places, where a precise normative

Is the explanation of relaxed ordering erroneous in cppreference?

限于喜欢 提交于 2020-01-22 10:50:11
问题 In the documentation of std::memory_order on cppreference.com there is an example of relaxed ordering: Relaxed ordering Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not impose an order among concurrent memory accesses. They only guarantee atomicity and modification order consistency. For example, with x and y initially zero, // Thread 1: r1 = y.load(std::memory_order_relaxed); // A x.store(r1, std::memory_order_relaxed); // B // Thread 2: r2 = x

Loop with a zero execution time

落花浮王杯 提交于 2019-12-17 08:55:24
问题 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. 回答1: 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. Examples For example the following code: int

Loop with a zero execution time

北城余情 提交于 2019-12-17 08:55:16
问题 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. 回答1: 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. Examples For example the following code: int

Loop with a zero execution time

吃可爱长大的小学妹 提交于 2019-11-27 07:02:19
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. Shafik Yaghmour 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. Examples For example the following code: int main() { int j = 0 ; for( int i = 0; i < 10000; ++i ) { ++j ; } } compiled with gcc 4.9 using

What exactly is the “as-if” rule?

核能气质少年 提交于 2019-11-25 22:20:58
问题 As the title says, What exactly is the \"as-if\" rule? An typical answer one would get is: The rule that allows any and all code transformations that do not change the observable behavior of the program From time to time we keep getting behaviors from certain implementations which are attributed to this rule. Many a times wrongly. So, what exactly is this rule. The standard does not clearly mention this rule as a section or paragraph, so what exactly falls under the purview of this rule? To