为什么有序数组比无序数组快呢?
来源:互联网 发布:js中获取小数点后两位 编辑:程序博客网 时间:2024/05/16 18:13
来自stackoverflow的题目Why is processing a sorted array faster than an unsorted array?
Here is a piece of C++ code that seems very peculiar. 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 long sum = 0; for (unsigned i = 0; i < 100000; ++i) { // Primary loop for (unsigned c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; } } double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC; std::cout << elapsedTime << std::endl; std::cout << "sum = " << sum << std::endl;}
- Without
std::sort(data, data + arraySize);
, the code runs in 11.54 seconds. - With the sorted data, the code runs in 1.93 seconds.
import java.util.Arrays;import java.util.Random;public class Main{ public static void main(String[] args) { // Generate data int arraySize = 32768; int data[] = new int[arraySize]; Random rnd = new Random(0); for (int c = 0; c < arraySize; ++c) data[c] = rnd.nextInt() % 256; // !!! With this, the next loop runs faster Arrays.sort(data); // Test long start = System.nanoTime(); long sum = 0; for (int i = 0; i < 100000; ++i) { // Primary loop for (int c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; } } System.out.println((System.nanoTime() - start) / 1000000000.0); System.out.println("sum = " + sum); }}
What is Branch Prediction?
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 guessed right, it continues on.
- If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.
If you guess right every time, the train will never have to stop.
If you guess wrong too often, the train will spend a lot of time stopping, backing up, and restarting.
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 guessed right, you continue executing.
- If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.
If you guess right every time, the execution will never have to stop.
If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.
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 guesses. If it goes one way every 3 times, you guess the same...
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:
if (data[c] >= 128) sum += data[c];
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:
T = branch takenN = branch not takendata[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...branch = N N N N N ... N N T T T ... T T T ... = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT (easy to predict)
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)
data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118, 14, 150, 177, 182, 133, ...branch = T, T, N, T, T, T, T, N, T, N, N, T, T, T, N ... = TTNTTTTNTNNTTTN ... (completely random - hard to predict)
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:
if (data[c] >= 128) sum += data[c];
with:
int t = (data[c] - 128) >> 31;sum += ~t & data[c];
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 ofdata[]
.)
Benchmarks: Core i7 920 @ 3.5 GHz
C++ - Visual Studio 2010 - x64 Release
// Branch - Randomseconds = 11.777// Branch - Sortedseconds = 2.352// Branchless - Randomseconds = 2.564// Branchless - Sortedseconds = 2.587
Java - Netbeans 7.1.1 JDK 7 - x64
// Branch - Randomseconds = 10.93293813// Branch - Sortedseconds = 5.643797077// Branchless - Randomseconds = 3.113581453// Branchless - Sortedseconds = 3.186068823
Observations:
- With the Branch: There is a huge difference between the sorted and unsorted data.
- With the Hack: There is no difference between sorted and unsorted data.
- In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.
A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example)
- 为什么有序数组比无序数组快呢?
- 为什么处理有序数组比无序数组快?
- 为什么处理有序数组比无序数组快?
- 为什么处理有序数组比无序数组快?
- 有序数组和无序数组
- 数组、数组操作、无序数组、有序数组、二分法
- 多线程为什么比单线程快呢?
- json_encode 二维有序数组和无序数组的区别
- 面试题:两个无序数组合并成一个有序数组
- 有序数组和无序数组的二分查找
- 为什么处理一个排序数组要比处理一个未排序的数组更快?
- 为什么处理一个排列数组比处理未排列数组更快?
- 为什么处理已排序数组比处理未排序数组更快?
- 为什么排序后的数组比没有排序过的数组运行快?
- 将两个无序数组合并为有序链表
- 【iOS开发】数组的去重(无序和有序)
- 为什么处理排序的数组要比非排序的快?(1)
- 为什么快速排序在数组的情况下比归并排序快
- ubuntu安装LAMP环境
- 关于内层DIV设置margin-top不起作用的解决方案
- Tomcat6.0数据源配置详解
- JMeter——运用BeanShell给自定义的变量动态赋值
- 第七周项目2电阻并联
- 为什么有序数组比无序数组快呢?
- 打印螺旋数组
- Word Search
- 别样JAVA学习(六)继承下(2.3)异常下
- JMeter——HTTP COOKIE Manager(cookie管理器)
- Visual Studio 2008破解激活升级方法
- PAT(A) 1006
- Cannot run program "ndk-build": Unknown reason
- 1 Maximum Product Subarray_Leetcode