简体   繁体   English

相同的代码比JS闭包库慢

[英]Identical code slower than JS closure library equivalent

I wanted to create a binary search algorithm with some modifications. 我想创建一个经过一些修改的二进制搜索算法。 So I grabbed the code from Google's closure library and started making these modifications. 因此,我从Google的封闭库中获取了代码,然后开始进行这些修改。 My modified version seemed slower than it should be so I slowly took out anything I thought could be affecting the speed. 我的修改版本似乎比应有的速度慢,所以我慢慢取出了我认为可能影响速度的所有内容。 What I was left with is a SIMPLER version of the binary search algorithm and it was still several times slower in both Chrome or firefox. 我剩下的是SIMPLER版本的二进制搜索算法,在Chrome或Firefox中,它的运行速度仍然慢了几倍。 What could be causing this? 是什么原因造成的? Take a look at this test page. 看一下这个测试页面。 Inspect the source to see what I'm talking about. 检查源,看看我在说什么。

https://dl.dropboxusercontent.com/s/4hhuq4biznv1jfd/SortedArrayTest.html https://dl.dropboxusercontent.com/s/4hhuq4biznv1jfd/SortedArrayTest.html

This is google's version. 这是Google的版本。

goog.array.binarySearch_ = function(arr, compareFn, isEvaluator, opt_target,
    opt_selfObj) {
  var left = 0;  // inclusive
  var right = arr.length;  // exclusive
  var found;
  while (left < right) {
    var middle = (left + right) >> 1;
    var compareResult;
    if (isEvaluator) {
      compareResult = compareFn.call(opt_selfObj, arr[middle], middle, arr);
    } else {
      compareResult = compareFn(opt_target, arr[middle]);
    }
    if (compareResult > 0) {
      left = middle + 1;
    } else {
      right = middle;
      // We are looking for the lowest index so we can't return immediately.
      found = !compareResult;
    }
  }
  // left is the index if found, or the insertion point otherwise.
  // ~left is a shorthand for -left - 1.
  return found ? left : ~left;
};

This is my version: 这是我的版本:

        var search = function(array, num){
            var left = 0;  // inclusive
            var right = array.length;  // exclusive
            while (left < right) {
                var middle = (left + right) >> 1;
                var midValue = array[midValue];
                if (num > midValue) {
                    left = middle + 1;
                } else {
                    right = middle;
                }
            }
            return left;
        };

Since people seem to think its something with the comparefn function...when you don't provide a comparer function to the binarySearch method it uses the following default compare function: 由于人们似乎对comparefn函数有所考虑...当您不向binarySearch方法提供比较器功能时,它将使用以下默认比较功能:

    goog.array.defaultCompare = function(a, b) {
      return a > b ? 1 : a < b ? -1 : 0;
    };

    goog.array.binarySearch = function(arr, target, opt_compareFn) {
  return goog.array.binarySearch_(arr,
      opt_compareFn || goog.array.defaultCompare, false /* isEvaluator */,
      target);
};

Please don't respond without looking at the code. 请不要在不看代码的情况下做出回应。 Guesses aren't very helpful. 猜测不是很有帮助。

Your implementation contains a bug. 您的实现包含一个错误。 It contains: 它包含:

var midValue = array[midValue]

which should be 应该是

var midValue = array[middle]

instead. 代替。

Apparently, your were unlucky enough for your data set not to expose the bug as an incorrect result, but just as a performance problem. 显然,您的数据集很不幸,没有将错误显示为错误的结果,而只是将其显示为性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM