在回答另一个问题时,我对此感到好奇。我很清楚
if( __builtin_expect( !!a, 0 ) ) {
// not likely
} else {
// quite likely
}
将通过向处理器提示/更改汇编代码顺序/某种魔法来使“很可能”分支更快(通常)。 (如果有人能澄清那也很棒的魔法)。
但这是否适用于 a) 内联 ifs、b) 变量和 c) 0 和 1 以外的值?即会
__builtin_expect( !!a, 0 ) ? /* unlikely */ : /* likely */;
或
int x = __builtin_expect( t / 10, 7 );
if( x == 7 ) {
// likely
} else {
// unlikely
}
或
if( __builtin_expect( a, 3 ) ) {
// likely
// uh-oh, what happens if a is 2?
} else {
// unlikely
}
有什么效果吗?所有这些都取决于目标架构吗?
最佳答案
您是否阅读了 GCC 文档?
Built-in Function: long __builtin_expect (long exp, long c)
You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (-fprofile-arcs), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect.
The return value is the value of exp, which should be an integral expression. The semantics of the built-in are that it is expected that exp == c. For example:
if (__builtin_expect (x, 0)) foo ();indicates that we do not expect to call foo, since we expect x to be zero. Since you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1)) foo (*ptr);when testing pointer or floating-point values.
稍微解释一下... __builtin_expect 对于传达您认为程序可能采用的分支特别有用。你问编译器如何使用这种洞察力——好吧,考虑一下这段代码:
if (x == 0)
return 10 * y;
else
return 39;
在机器代码中,CPU 通常会被要求“转到”另一行(这需要时间,并且取决于 CPU 可能会阻止其他执行优化 - 即低于机器代码级别 - 例如,请参阅分支标题在 http://en.wikipedia.org/wiki/Instruction_pipeline 下),或者调用其他代码,但实际上并没有一个 if/else 概念,其中 true 和 false 代码是相等的......您必须分支才能找到其中一个或另一个的代码。完成的方式基本上是伪代码:
test whether x is 0
if it was goto else_return_39
return 10 * y
else_return_39:
return 39
鉴于大多数 CPU 跟随 goto 到 else_return_39: 标签比仅仅下降到 return 10 * y 慢,代码因为“真”分支将比假分支更快到达。当然,机器码可以测试x是否不为0,把“假”码(return 39)放在前面,从而反转性能特征。
这是 __builtin_expect 控制的 - 您可以告诉编译器将 true 或 false 分支放在需要较少分支才能到达它的地方,从而获得微小的性能提升。
But does this work for a) inline ifs, b) variables and c) values other than 0 and 1?
a) 周围的函数是否被内联并不会改变 if 语句出现的分支的需要(除非优化器看到 if 语句的条件tests 总是 true 或 false 并且只有一个分支永远无法运行)。因此,它同样适用于内联代码。
[ 您的评论表明您对条件表达式感兴趣 - a ? b : c - 我不确定 - 在 Can I use GCC's __builtin_expect() with ternary operator in C 上对这个问题有一个有争议的答案这可能以一种或另一种方式证明是有见地的,或者是进一步探索的基础]
b) 变量 - 你假设:
int x = __builtin_expect( t / 10, 7 );
if( x == 7 ) {
这是行不通的——编译器没有义务将这些期望与变量相关联,并在下次看到 if 时记住它们。您可以使用 gcc -S 验证这一点(就像我对 gcc 3.4.4 所做的那样)以生成汇编语言输出:无论预期值如何,程序集都不会改变。
c) 0 和 1 以外的值
它适用于整数 (long) 值,所以是的。上面引用的文档的最后一段解决了这个问题,特别是:
you should use constructions such as
if (__builtin_expect (ptr != NULL, 1)) foo (*ptr);when testing pointer or floating-point values.
为什么?好吧,如果指针类型大于 long,那么调用 __builtin_conversion(long, long) 将有效地切掉一些不太重要的位并且无法合并其余位在测试中。同样,浮点值可能大于 long,并且转换不会产生您期望的结果。通过使用诸如 ptr != NULL 之类的 bool 表达式(假设 true 转换为 1L 和 false 为 0),您一定会得到预期结果。
关于c++ - GCC 的 __builtin_expect 能走多远?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468042/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
在MichaelHartl的RailsTutorial中,许多示例使用expect()方法。这是cucumber步骤定义中的一个这样的例子:Then/^sheshouldseeherprofilepage$/doexpect(page).tohave_title(@user.name)end同样的例子可以写成同样的效果:Then/^sheshouldseeherprofilepage$/dopage.shouldhave_title(@user.name)end为什么要使用expect()?它增加了什么值(value)? 最佳答案
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t
我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc
我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat