bestpair

Benchmark created by Carroll Yu on


Test runner

Ready to run.

Testing in
TestOps/sec
a
    function stock_picker(stockPrices) {
      var profitDays = [];
      var bestProfit = 0;

      for (x = 0; x < stockPrices.length; x++) {
        for (y = 0; y < stockPrices.length; y++) {
          if (x < y && (stockPrices[y] - stockPrices[x] > bestProfit)) {
            bestProfit = stockPrices[y] - stockPrices[x];
            profitDays = [x, y];
          }
        }
      }
      return profitDays;
    }

    var numbers = [37, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 8099, 8083, 9249, 8284, 6705, 3153, 7862, 487, 9510, 158, 5504, 5117, 3685, 7769, 4303, 4641, 563, 7776, 6019, 3504, 449, 5733, 3990, 699, 9309, 9517, 3384, 6707, 4468, 8937, 9179, 9137, 2248, 3655, 4875, 3473, 6918, 3698, 3497, 7526, 3260, 7439, 8145, 4118, 3967, 8080, 4348, 9493, 5397, 4091, 7798, 2369, 917, 2151, 2697, 4302, 6982, 6141, 2954, 1855, 7074, 7503, 9520, 7172, 4679, 3239, 5931, 7897, 8468, 452, 4734, 6746, 4166, 8260, 8537, 4103, 465, 4708, 6280, 9760, 5348, 5369, 653, 4627, 134, 1791, 9304, 6327, 5297, 5288, 1390, 8497, 5627, 2109, 4203, 4196, 1299, 3367, 7344, 7991, 1323, 6200, 4182, 4621, 9541, 1508, 475, 9802, 1348, 2245, 1371, 1, 1007, 1342, 1735, 3713, 6647, 999, 6211, 7361, 7998, 8451, 6206, 6908, 184, 8380, 9599, 1710, 7921, 3524, 7989, 9337];
    var pairs = stock_picker(numbers);
    console.log(pairs);
ready
b
    function stock_picker(stockPrices) {
      var profitDays = [0, 0];
      var low = 0;
      var high = 0;
      var bestPrice = 0;

      for (i = 0; i < stockPrices.length; i++) {
        if (stockPrices[i] < stockPrices[low]) {
          low = i;
          high = i;
        } else if (stockPrices[high] < stockPrices[i]) {
          high = i;
          if (stockPrices[i] - stockPrices[low] > bestPrice) {
            bestPrice = stockPrices[i] - stockPrices[low];
            profitDays = [low, high];
          }
        }
      }
      return profitDays;
    }

    var numbers = [37, 4279, 3426, 6178, 8882, 830, 9406, 3013, 1026, 3924, 284, 8346, 147, 7093, 2212, 2526, 2528, 1494, 3792, 4555, 9530, 4775, 1627, 2424, 637, 6769, 2681, 1245, 6946, 9497, 4186, 382, 2480, 8672, 5385, 7740, 2319, 8187, 8369, 7392, 3079, 8431, 6834, 1968, 2116, 6036, 3106, 8849, 1154, 9162, 4170, 6052, 7048, 9318, 6872, 517, 8027, 9558, 7686, 9592, 6810, 37, 4830, 4283, 4614, 704, 3451, 1783, 55, 1276, 7426, 9986, 670, 1121, 3330, 7559, 9229, 9937, 6337, 8692, 8948, 6261, 2495, 7042, 7179, 8198, 2118, 4431, 9175, 7578, 1177, 5773, 6096, 823, 2299, 2119, 7108, 7369, 5475, 1931, 8872, 6014, 4423, 236, 3283, 7324, 6056, 3045, 6174, 4856, 4880, 6023, 2190, 6183, 5484, 3026, 9698, 8099, 8083, 9249, 8284, 6705, 3153, 7862, 487, 9510, 158, 5504, 5117, 3685, 7769, 4303, 4641, 563, 7776, 6019, 3504, 449, 5733, 3990, 699, 9309, 9517, 3384, 6707, 4468, 8937, 9179, 9137, 2248, 3655, 4875, 3473, 6918, 3698, 3497, 7526, 3260, 7439, 8145, 4118, 3967, 8080, 4348, 9493, 5397, 4091, 7798, 2369, 917, 2151, 2697, 4302, 6982, 6141, 2954, 1855, 7074, 7503, 9520, 7172, 4679, 3239, 5931, 7897, 8468, 452, 4734, 6746, 4166, 8260, 8537, 4103, 465, 4708, 6280, 9760, 5348, 5369, 653, 4627, 134, 1791, 9304, 6327, 5297, 5288, 1390, 8497, 5627, 2109, 4203, 4196, 1299, 3367, 7344, 7991, 1323, 6200, 4182, 4621, 9541, 1508, 475, 9802, 1348, 2245, 1371, 1, 1007, 1342, 1735, 3713, 6647, 999, 6211, 7361, 7998, 8451, 6206, 6908, 184, 8380, 9599, 1710, 7921, 3524, 7989, 9337];
    var pairs = stock_picker(numbers);
    console.log(pairs);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Carroll Yu on