- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
注意:这里根本没有多线程。刚刚优化了单线程代码。
函数调用 introduces a sequence point . (显然。)
是否允许编译器(如果优化器内联函数) 允许在函数指令之前/之后移动/混合任何指令? (只要它可以明显地“证明”没有可观察到的效果。)
说明背景:
现在,有一个 nice article wrt。一个 C++ 的基准测试类,作者在其中声明:
The code we time won’t be rearranged by the optimizer and will always lie between those start / end calls to now(), so we can guarantee our timing will be valid.
我问他怎么能确定,尼克回答说:
You can check the comment in this answer https://codereview.stackexchange.com/a/48884. I quote : “I would be careful about timing things that are not functions because of optimizations that the compiler is allowed to do. I am not sure about the sequencing requirements and the observable behavior understanding of such a program. With a function call the compiler is not allowed to move statements across the call point (they are sequenced before or after the call).”
What we do is basically abstract the callable (function, lambda, block of code surrounded by lambda) and have a signle call
callable(factor)
inside themeasure
structure that acts as a barrier (not the barrier in multithreading, I believe I convey the message).
我对此不太确定,尤其是引用:
With a function call the compiler is not allowed to move statements across the call point (they are sequenced before or after the call).
现在,我一直认为,当优化器内联某个函数时(在(简单)基准测试场景中很可能是这种情况),只要不影响它,它就可以自由地重新排列它喜欢的任何东西可观察到的行为。
也就是说,就语言/优化器而言,这两个片段完全相同:
void f() {
// do stuff / Multiple statements
}
auto start = ...;
f();
auto stop = ...;
对比
auto start = ...;
// do stuff / Multiple statements
auto stop = ...;
最佳答案
Now, I was always under the impression that when an optimizer inlines some function (which may very well be the case in a (simple) benchmark scenario), it is free to rearrange whatever it likes as long as it does not affect observable behavior.
绝对是。理论上,优化器甚至不需要内联它。
但是,计时函数是可观察到的行为——具体来说,它们是系统部分的 I/O。如果以与其他 I/O 调用不同的顺序执行,优化器无法知道 I/O 会产生相同的结果(显然不会),这可能包括不明显的事情,例如可以调用系统调用的内存分配调用获取他们的内存。
这基本上意味着,总的来说,对于大多数函数调用,优化器无法进行大量重新安排,因为可能涉及大量无法推理的状态。
此外,优化器无法真正知道重新安排函数调用实际上会使代码运行得更快,并且会使调试变得更加困难,因此他们没有很大的动力去搞砸按照程序规定的顺序。
基本上,理论上优化器可以做到这一点,但实际上不会,因为这样做将是一项艰巨的任务,不会带来太多好处。
只有当您的基准测试相当琐碎或几乎完全由整数加法等原始操作组成时,您才会遇到这样的情况——在这种情况下,您无论如何都需要检查程序集。
关于c++ - 是否允许 C++ 优化器跨函数调用移动语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593156/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!