Why is processing a sorted array faster than an unsorted array(Stackoverflow)
What is Branch Prediction?Consider a railroad junction: Image Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication. You are the operator of a junction and you hear a train coming. You have no idea which way it will go. You stop the train to ask the captain which direction he wants. And then you set the switch appropriately. Trains are heavy and have a lot of inertia. So they take forever to start up and slow down. Is there a better way? You guess which direction the train will go!
If you guess right every time, the train will never have to stop. Consider an if-statement: At the processor level, it is a branch instruction: You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path. Modern processors are complicated and have long pipelines. So they take forever to "warm up" and "slow down". Is there a better way? You guess which direction the branch will go!
If you guess right every time, the execution will never have to stop. This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment. So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work. Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless. Further reading: "Branch predictor" article on Wikipedia. As hinted from above, the culprit is this if-statement:
Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement. This is very friendly to the branch predictor since the branch consecutively goes the same direction many times.Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction. Quick visualization:
However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data.Thus there will probably be around 50% misprediction. (no better than random guessing)
So what can be done? If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance. Replace:
with:
This eliminates the branch and replaces it with some bitwise operations. (Note that this hack is not strictly equivalent to the original if-statement. But in this case, it's valid for all the input values of Benchmarks: Core i7 920 @ 3.5 GHz C++ - Visual Studio 2010 - x64 Release
Java - Netbeans 7.1.1 JDK 7 - x64
Observations:
A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example) Update :
This goes to show that even mature modern compilers can vary wildly in their ability to optimize code... |
|||
每一次CPU运行这个条件推断时,CPU都可能跳转到循环開始处的指令。即不运行if后的指令。
使用分支预測技术。当处理已经排序的数组时。在若干次data[c]>=128都不成立时(或第一次不成立时。取决于分支预測的实现),CPU预測这个分支是始终会跳转到循环開始的指令时。这个时候CPU将保持有效的运行,不须要又一次等待到新的地址取指。相同。当data[c]>=128条件成立若干次后,CPU也能够预測这个分支是不必跳转的。那么这个时候CPU也能够保持高效运行。 |
|
最新文章
- TypeScript中的枚举类型
- .NET软件开发与常用工具清单(转)
- BZOJ4471 : 随机数生成器Ⅱ
- cd hit使用
- icp算法基本思想
- CSS学习篇核心之——盒子模型
- Codeforce Gym 100015I Identity Checker 暴力
- 【7】用Laravel5.1开发一个简单的博客系统
- 【转】Alsa音频编程【精华】
- Android Realm数据库使用指南
- Error in library(DESeq2) : 不存在叫‘DESeq2’这个名字的程辑包
- springboot+mybatis+ehcache实现缓存数据
- Python实现RSA无填充加密,兼容BouncyCastle
- 项目总结四:神经风格迁移项目(Art generation with Neural Style Transfer)
- 30天代码day3 Intro to Conditional Statements
- 2018年Java生态行业报告
- ORACLE-osi分层模型.md
- docker容器多服务(不推荐)
- Makefile 中@是什么意思
- Linux正则与文本处理工具(10)