fork download
Array.prototype.swap=function(a, b)
{
	var tmp=this[a];
	this[a]=this[b];
	this[b]=tmp;
}
  
function partition(array, begin, end, pivot)
{
	var piv=array[pivot];
	array.swap(pivot, end-1);
	var store=begin;
	var ix;
	for(ix=begin; ixbegin) {
		var pivot=begin+Math.floor(Math.random()*(end-begin));

		pivot=partition(array, begin, end, pivot);

		qsort(array, begin, pivot);
		qsort(array, pivot+1, end);
	}
}

function quick_sort(array) {
	qsort(array, 0, array.length);
}

function shuffle(array) {
  var len = array.length;
  var i = len;
  while (i--) {
    var p = parseInt(Math.random() * len);
    var t = array[i];
    array[i] = array[p];
    array[p] = t;
  }
}
 
function Item(param1, param2, param3, value) {
  this.param1 = param1;
  this.param2 = param2;
  this.param3 = param3;
  this.value = value;
  this.key = [param1, param2, param3].toString();
  this.toString = function() {
    return this.key;
  }
}
  
function populateTestData() {
  var res = [];
  var guid = ['6aa20b7a-da8a-466d-8f0e-92e49ca62759', 'e2cd3214-1097-46fc-931c-03d5becc3318', 'a9d71320-aeed-445f-9741-bf61d45a565a'];
  for (var p1 = 0; p1 < guid.length; p1++) {
    for (var p2 = 0; p2 < 17; p2++) {
      for (var p3 = 0; p3 < 9; p3++) {
        var item = new Item(guid[p1], 'parameter' + p2, p3, [p1, p2, p3].toString());
        res.push(item);
      }
    }
  }
  return res;
}
 
function listToMap(lst) {
  var map = {};
  for (var i = 0; i < lst.length; i++) {
    var item = lst[i];
    if (item.value != null) {
      var key = [item.param1, item.param2, item.param3];
      map[key] = item.value;
    }
  }
  return map;
}
  
function arrayTranspose_(data) {
  if (data.length == 0 || data[0].length == 0) {
    return null;
  }

  var ret = [];
  var retLen = data[0].length;
  for (var i = 0; i < retLen; ++i) {
    ret.push([]);
  }

  for (var i = 0; i < data.length; ++i) {
    for (var j = 0; j < retLen; ++j) {
      ret[j][i] = data[i][j];
    }
  }

  return ret;
}
  
function adjustData_(inData) {
  var colData = inData.slice();
//  var tz = Session.getTimeZone();
  for (var c = 0; c < colData.length; c++) {
    var column = colData[c];
    var colLen = column.length;
    if (colLen > 0 && column[0].length > 0 && column[0][0] instanceof Date) {
      for (var i = 0; i < colLen; i++) {
        var value = column[i][0];
        if (value instanceof Date) {
//          column[i][0] = Utilities.formatDate(value, tz, 'yyyy-MM-dd HH:mm');
          column[i][0] = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate() + ' ' + value.getHours() + ':' + value.getMinutes();
        }
      }
    }
  }
  var res = [];
  var rowData = arrayTranspose_(colData);
  for (var i = 0; i < rowData.length; i++) {
    var row = rowData[i];
    var rowCorrect = [];
    for (var j = 0; j < row.length; j++) {
      var rr = row[j];
      var r = '';
      if (rr != undefined) {
        r = rr[0];
      }
      rowCorrect.push(r);
    }
    res.push(rowCorrect);
  }
  return res;
}
  
function populateTestData01() {
  var count = 127;
  var now = new Date().valueOf();
  var lstDate = [];
  var lstValue01 = [];
  var lstValue02 = [];
  for (var i = 0; i < count; i++) {
    var dateValue = new Date(now + i * 60000);
    lstDate.push([dateValue]);
    lstValue01.push([Math.random()]);
    lstValue02.push([Math.random()]);
  }
  return [lstDate, lstValue01, lstValue02];
}
  
function test() {
  var count = 11;
  var lstData = populateTestData();
  shuffle(lstData);
  var start = new Date();
  var lstPreventOptimize = [];
  for (var i = 0; i < count; i++) {
    var mapData = listToMap(lstData);
    lstPreventOptimize.push(mapData);
  }
  var timeLstToMap = new Date() - start;
  var lstCopy00 = lstData.slice();
  var start = new Date();
  quick_sort(lstCopy00);
  var timeQuickSort = new Date() - start;
  var lstCopy01 = lstData.slice();
  var start = new Date();
  lstCopy01.sort();
  var timeSort = new Date() - start;
  var lstData01 = populateTestData01();
  var start = new Date();
  for (var i = 0; i < count; i++) {
    var adjustedData = adjustData_(lstData01);
    lstPreventOptimize.push(adjustedData);
  }
  var timeAdjust = new Date() - start;
  return { timeLstToMap: timeLstToMap, timeQuickSort: timeQuickSort, timeSort : timeSort, timeAdjust: timeAdjust, dummy: lstPreventOptimize };
}
  
function runTest() {
  var start = new Date();
  var res = test();
  var timeTotal = new Date() - start;
  var resStr = 'Complete Time: ' + timeTotal + 'ms, List To Map Time: ' + res.timeLstToMap + 'ms, Adjust Time: ' + res.timeAdjust + 'ms, QuickSort Time: ' + res.timeQuickSort + 'ms, Sort Time: ' + res.timeSort + 'ms';
  return resStr;
}

print(runTest());
Success #stdin #stdout 0.67s 214976KB
stdin
Standard input is empty
stdout
Complete Time: 346ms, List To Map Time: 67ms, Adjust Time: 44ms, QuickSort Time: 31ms, Sort Time: 9ms