branch-prediction

What is the effect of ordering if…else if statements by probability?

二次信任 提交于 2019-11-27 00:00:52
问题 Specifically, if I have a series of if ... else if statements, and I somehow know beforehand the relative probability that each statement will evaluate to true , how much difference in execution time does it make to sort them in order of probability? For example, should I prefer this: if (highly_likely) //do something else if (somewhat_likely) //do something else if (unlikely) //do something to this?: if (unlikely) //do something else if (somewhat_likely) //do something else if (highly_likely

Portable branch prediction hints

懵懂的女人 提交于 2019-11-26 22:44:56
问题 Is there any portable way of doing branch prediction hints? Consider the following example: if (unlikely_condition) { /* ..A.. */ } else { /* ..B.. */ } Is this any different than doing: if (!unlikely_condition) { /* ..B.. */ } else { /* ..A.. */ } Or is the only way to use compiler specific hints? (e.g. __builtin_expect on GCC) Will compilers treat the if conditions any differently based on the ordering of the conditions? 回答1: The canonical way to do static branch prediction is that if is

Is there a compiler hint for GCC to force branch prediction to always go a certain way?

天大地大妈咪最大 提交于 2019-11-26 21:25:45
For the Intel architectures, is there a way to instruct the GCC compiler to generate code that always forces branch prediction a particular way in my code? Does the Intel hardware even support this? What about other compilers or hardwares? I would use this in C++ code where I know the case I wish to run fast and do not care about the slow down when the other branch needs to be taken even when it has recently taken that branch. for (;;) { if (normal) { // How to tell compiler to always branch predict true value? doSomethingNormal(); } else { exceptionalCase(); } } As a follow on question for

Is “IF” expensive?

耗尽温柔 提交于 2019-11-26 14:52:58
I can't, for the life of me, remember what exactly our teacher said that day and I'm hoping you would probably know. The module is "Data Structures and Algorithms" and he told us something along the lines of: The if statement is the most expensive [something]. [something] registers [something]. Yes, I do have a horrible memory and I'm really really sorry, but I've been googling for hours and nothing has come up. Any ideas? At the very lowest level (in the hardware), yes, if s are expensive. In order to understand why, you have to understand how pipelines work. The current instruction to be

Performance optimisations of x86-64 assembly - Alignment and branch prediction

房东的猫 提交于 2019-11-26 10:36:02
问题 I’m currently coding highly optimised versions of some C99 standard library string functions, like strlen() , memset() , etc, using x86-64 assembly with SSE-2 instructions. So far I’ve managed to get excellent results in terms of performance, but I sometimes get weird behaviour when I try to optimise more. For instance, adding or even removing some simple instructions, or simply reorganising some local labels used with jumps completely degrades the overall performances. And there’s absolutely

Why is a conditional move not vulnerable for Branch Prediction Failure?

大兔子大兔子 提交于 2019-11-26 07:57:20
问题 After reading this post (answer on StackOverflow) (at the optimization section), I was wondering why conditional moves are not vulnerable for Branch Prediction Failure. I found on an article on cond moves here (PDF by AMD). Also there, they claim the performance advantage of cond. moves. But why is this? I don\'t see it. At the moment that that ASM-instruction is evaluated, the result of the preceding CMP instruction is not known yet. 回答1: Mis-predicted branches are expensive A modern

Is there a compiler hint for GCC to force branch prediction to always go a certain way?

巧了我就是萌 提交于 2019-11-26 07:56:14
问题 For the Intel architectures, is there a way to instruct the GCC compiler to generate code that always forces branch prediction a particular way in my code? Does the Intel hardware even support this? What about other compilers or hardwares? I would use this in C++ code where I know the case I wish to run fast and do not care about the slow down when the other branch needs to be taken even when it has recently taken that branch. for (;;) { if (normal) { // How to tell compiler to always branch

Why is processing a sorted array faster than processing an unsorted array?

最后都变了- 提交于 2019-11-26 05:40:37
问题 Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data miraculously makes the code almost six times faster: #include <algorithm> #include <ctime> #include <iostream> int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c < arraySize; ++c) data[c] = std::rand() % 256; // !!! With this, the next loop runs faster. std::sort(data, data + arraySize); // Test clock_t start = clock(); long

Is “IF” expensive?

被刻印的时光 ゝ 提交于 2019-11-26 03:47:20
问题 I can\'t, for the life of me, remember what exactly our teacher said that day and I\'m hoping you would probably know. The module is \"Data Structures and Algorithms\" and he told us something along the lines of: The if statement is the most expensive [something]. [something] registers [something]. Yes, I do have a horrible memory and I\'m really really sorry, but I\'ve been googling for hours and nothing has come up. Any ideas? 回答1: At the very lowest level (in the hardware), yes, if s are