为什么短路逻辑运算符应该更快

时间:2017-12-26 20:02:52

标签: performance logical-operators instructions branch-prediction

这个问题不是关于优化代码,而是关于短路逻辑运算符和普通逻辑运算符的性能差异的技术问题,这可能归结为它们在硬件级别上的执行方式。

基本上,逻辑<!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Library Genesis</title> <style> #message { width: 400px; margin: 0px auto; padding: 10px 20px; text-align: center; background-color: #0f9d58; color: #fff; border-radius: 3px; } </style> <script src="/jquery-latest.min.js"></script> <script src="/clipboard.min.js"></script> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script></head><body><script> $(document).ready(function() { function appendMessage(argument) { var div = $('<div>').attr('id', 'message').text('Ad block is installed and active. Please support us by disabling it.'); var add = $('table').before(div); } setTimeout(function(){ if($("ins").css('display') == "none") { appendMessage(); } }, 500); }); </script><table width=1000 align="center" border=0> <tr> <td align="left" valign="top" bgcolor="#F5F6CE" rowspan=2><font size=2 color="grey">Advertising:</font><br><div id="ads10"> <!-- gen_sky3 --> <ins class="adsbygoogle" style="display:inline-block;width:300px;height:600px" data-ad-client="ca-pub-1513624324396300" data-ad-slot="9137517849"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></td> <td rowspan=2 valign="top" bgcolor="#F5F6CE"><font size=2 color="grey">BitCoin: <a href="bitcoin:1PiZNj7uhejkdMg5ycbqdGduKKafy8eubm?label=libgen">1PiZNj7uhejkdMg5ycbqdGduKKafy8eubm</a></font><br><div id="ads9"> <!-- gen_sky2 --> <ins class="adsbygoogle" style="display:inline-block;width:300px;height:600px" data-ad-client="ca-pub-1513624324396300" data-ad-slot="3422388247"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></td> <td align='center' rowspan=2 valign='top'><a href='http://libgen.io/get.php?md5=f49b28272e405bf000fe72e5d93c4ea0&key=ON32H85E8S3NRX3D'><h2>GET</h2></a></br><a href='/book/index.php?md5=F49B28272E405BF000FE72E5D93C4EA0&oftorrent='>Download via torrent </a><input id="textarea-example" value="John E. Jackson-The Biology of Apples and Pears (The Biology of Horticultural Crops)-Cambridge University Press (2003).pdf" type="text" size="9"><button class="btn-clipboard" data-clipboard-target="#textarea-example"> (need rename file)</button><script>new Clipboard(".btn-clipboard");</script></br><textarea rows='13' name='bibtext' id='bibtext' readonly cols='40'>@book{book:222949, title = {The Biology of Apples and Pears (The Biology of Horticultural Crops)}, author = {John E. Jackson}, publisher = {Cambridge University Press}, isbn = {0521380189,9780521380188,9780511067464,0511067461}, year = {2003}, series = {}, edition = {}, volume = {}, url = {http://gen.lib.rus.ec/book/index.php?md5=f49b28272e405bf000fe72e5d93c4ea0}}</textarea> <br><img src='https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fgen.lib.rus.ec%2Fbook%2Findex.php%3Fmd5%3Df49b28272e405bf000fe72e5d93c4ea0&choe=UTF-8' title='Link to Libgen' /></td> <td align="left" valign="top" bgcolor="#F5F6CE" rowspan=2><div id="ads18"> <!-- gen_sky_160x600_2 --> <ins class="adsbygoogle" style="display:inline-block;width:160px;height:600px" data-ad-client="ca-pub-1513624324396300" data-ad-slot="4896944640"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></td> </tr> </table></body></html> AND需要一个周期,而短路评估使用分支,可以采用不同的周期。现在我知道分支预测器可以使这个评估有效但我不知道它比1个循环更快吗?

是的,如果右操作数是昂贵的,那么试图不评估它是有益的。但对于像OR这样的简单条件,假设这些是原子变量,非短路逻辑运算符可能会更快地执行。我是对的吗?

我认为短路逻辑运算符使用分支(没有官方来源,只是自我思考),因为在按顺序执行指令时如何进行跳转?

2 个答案:

答案 0 :(得分:0)

这已经很晚了,但是由于尚未得到答复(...),因此我将尝试一下。

您已经指出了分支预测,这在本质上是正确的。 现代硬件上还存在其他与硬件相关的问题,主要与指令级并行性和操作相互依赖性有关。

如果a为假,短路操作员需要对A和THEN B进行评估,而对B不进行评估。由于推测性执行,这使我们回到分支和CPU管道刷新。如果需要连续检查更多的条件,这可能会导致/获得更高的成本。另一方面,由于存在多个物理ALU / FPU / AGU等,CPU可以在同一时钟周期内取消“许多”指令,因此使用非短路操作可以降低成本。

为了说明这一点,让我们看一下Assembly中最简单的情况:

a && b: 

cmp    a, 0
jne    LABEL_A
---more code---
LABEL_A:
cmp    b, 0
jne    RETURN_LABEL
 ---more code--- 

与...相反(假设使用setb之类的指令来钳制[0,1])

a & b 

and   a, a, b
cmp   a, 0
jne   RETURN_LABEL
---more code---

这在生成的程序集本身中应该是不言而喻的。 但是,是的,您说的很对,在A为假的情况下,绝对应该使用短路以避免昂贵的计算B。但是即使那样,CPU仍可能会推测性地对B执行测试。因此,基本上,很简单地说,您“只能通过使用短路运算符来使情况变得更糟!”。

答案 1 :(得分:-1)

短路逻辑运算符比普通运算符快,这与低级HW无关。只要你有这个代码

if((a == b) and (c == d)){
    do some thing;
}

如果和运算符是一个普通运算符,那么在运行时,将评估和运算的两侧,这意味着a == bc == d的值都将被清除。这里有一个问题,如果a == bfalse,那么我们就不需要将c == d作为整体检查,并且始终是false。短路和操作员使用这个技巧,如果在这种情况下发现a == bfalse,那么它将不会检查条件的剩余部分,因为结果是已知的而不消耗CPU周期来检查没有条件的条件。

相关问题