文档章节

JAVA常见问题其一:Why is it faster to process a sorted array than an unsorted array?

-_-struggle
 -_-struggle
发布于 2016/08/08 12:17
字数 865
阅读 3
收藏 0

You are the operator of a junction and you hear a train coming. You have no idea which way it is supposed to go. You stop the train to ask the driver which direction they want. 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:

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 taken
N = branch not taken

data[] = 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 of data[].)

Benchmarks: Core i7 920 @ 3.5 GHz

C++ - Visual Studio 2010 - x64 Release

//  Branch - Random
seconds = 11.777

//  Branch - Sorted
seconds = 2.352

//  Branchless - Random
seconds = 2.564

//  Branchless - Sorted
seconds = 2.587

Java - Netbeans 7.1.1 JDK 7 - x64

//  Branch - Random
seconds = 10.93293813

//  Branch - Sorted
seconds = 5.643797077

//  Branchless - Random
seconds = 3.113581453

//  Branchless - Sorted
seconds = 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)

 

本文转载自:http://stackoverflow.com/questions/11227809/why-is-it-faster-to-process-a-sorted-array-than-an-unsor

共有 人打赏支持
-_-struggle
粉丝 3
博文 210
码字总数 50914
作品 0
苏州
后端工程师
私信 提问
67. Two Sum II - Input array is sorted - LeetCode

Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nums[i],这样就实现了降维了 Java实现: 别人的实现: https://l...

yysue
2018/08/16
0
0
153. Find Minimum in Rotated Sorted Array-LeetCode

Question 153. Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成[4,5,6,1,2,3],把1求出来 思路:遍历,如果当前元素比...

yysue
2018/07/07
0
0
TymeacDSE 2.1 发布

TymeacDSE是一个Java的fork-join应用服务器,采用分而治在一个嵌入式或 RMI 的配置方法。 TymeacDSE is a Java Fork-Join application server employing the Divide-and-Conquer methodolog......

红薯
2011/09/06
975
0
bytebuffer vs c++ malloc

Java equivalents of malloc(), new, free() and delete (ctd) Continued from our introduction to memory management operators in C/C++ and Java. A Java equivalent of the malloc() fu......

不道归来
2017/10/23
0
0
MyBatis传入参数与parameterType

传入简单类型 Java代码: MAPPER : 2. 传入List JAVA代码: 单独传入list时,foreach中的collection必须是list,不不管变量的具体名称是什么。比如这里变量名为idList, collection却是list。 ...

为了美好的明天
2018/05/17
0
0

没有更多内容

加载失败,请刷新页面

加载更多

解决各浏览器向url中传中问参数的问题

https://www.cnblogs.com/godtrue/p/4333262.html 后台的处理代码 public static String getUrlnewName( String oldName) {String newName = "";try {String agent = inv.get......

踏破铁鞋无觅处
12分钟前
1
0
微信支付携带证书请求

package utils.wechat; import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.security.KeyStore; import javax.net.ssl.SSLContext;......

猿神出窍
20分钟前
1
0
1093 - You can't specify target table 'xxx' for update in FROM clause, Time: 0.002000s

1093 - You can't specify target table 'xxx' for update in FROM clause, Time: 0.002000s 根据结果集在b_order_copy1 表中删除 DELETE FROM b_order_copy1 WHERE Id in ( SELECT Id FRO......

lwenhao
20分钟前
1
0
JavaScriptCore全面解析

本文由云+社区发表 作者:殷源,专注移动客户端开发,微软Imagine Cup中国区特等奖获得者 JavaScript越来越多地出现在我们客户端开发的视野中,从ReactNative到JSpatch,JavaScript与客户端相...

腾讯云加社区
24分钟前
1
0
Jmeter参数的AES加密使用

在Jmeter日常实践中,大家应该都遇到过接口传参需要加密的情况。以登陆为例,用户名和密码一般都需要进行加密传输,在服务端再进行解密,这样安全系数会更高,但在使用jmeter进行接口测试的时...

程序猿拿Q
36分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部