Don't store intermediate statistic data.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 24 Sep 2014 17:59:14 +0300
changeset 127 4cc0421184ac
parent 126 e634761d0432
child 128 673743a1290d
Don't store intermediate statistic data.
2048.html
--- a/2048.html	Wed Sep 24 15:25:11 2014 +0300
+++ b/2048.html	Wed Sep 24 17:59:14 2014 +0300
@@ -803,7 +803,10 @@
 
     ui.report.stat = function() {
       /* console.profile(); */
-      var stats = [];
+      var scoreChecked = document.getElementById('report-score').checked;
+      var turnChecked = document.getElementById('report-turn').checked;
+      var speedChecked = document.getElementById('report-speed').checked;
+      var histo = {};
       var cnt = parseInt(document.getElementById('stat-count-limit').value);
       if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
         cnt = 100;
@@ -836,39 +839,33 @@
         }
         var tsTo = new Date().getTime();
         var stat = board.score(brd);
-        stat.turn = turn;
-        stat.ts = tsTo - tsFrom;
-        stats.push(stat);
+        var speed = turn*1000/(tsTo - tsFrom);
+        var score = stat.score;
+        var max = stat.max;
+        if ( ! histo[max]) {
+          histo[max] = { n: 1, minSpeed: speed, meanSpeed: speed, maxSpeed: speed, minTurn: turn, meanTurn: turn, maxTurn: turn, minScore: score, meanScore: score, maxScore: score };
+        } else {
+          var row = histo[max];
+          row.n++;
+          if (scoreChecked) {
+            row.minScore = Math.min(row.minScore, score);
+            row.meanScore += score;
+            row.maxScore = Math.max(row.maxScore, score);
+          }
+        if (speedChecked) {
+            row.minSpeed = Math.min(row.minSpeed, speed);
+            row.meanSpeed += speed;
+            row.maxSpeed = Math.max(row.maxSpeed, speed);
+          }
+          if (turnChecked) {
+            row.minTurn = Math.min(row.minTurn, turn);
+            row.meanTurn += turn;
+            row.maxTurn = Math.max(row.maxTurn, turn);
+          }
+        }
         if (tsTo - tsLimitFrom >= tsLimit)
           break;
       }
-      var scoreChecked = document.getElementById('report-score').checked;
-      var turnChecked = document.getElementById('report-turn').checked;
-      var speedChecked = document.getElementById('report-speed').checked;
-      var histo = {};
-      for (i = stats.length-1; i >= 0; i--) {
-        var stat = stats[i];
-        if ( ! histo[stat.max])
-          histo[stat.max] = { n: 0, minSpeed: Infinity, meanSpeed: 0, maxSpeed: 0, minTurn: Infinity, meanTurn: 0, maxTurn: 0, minScore: Infinity, meanScore: 0, maxScore: 0 };
-        var row = histo[stat.max];
-        row.n++;
-        if (scoreChecked) {
-          row.minScore = Math.min(row.minScore, stat.score);
-          row.meanScore += stat.score;
-          row.maxScore = Math.max(row.maxScore, stat.score);
-        }
-        if (turnChecked) {
-          row.minTurn = Math.min(row.minTurn, stat.turn);
-          row.meanTurn += stat.turn;
-          row.maxTurn = Math.max(row.maxTurn, stat.turn);
-        }
-        if (speedChecked) {
-          var speed = (stat.turn * 1000.0) / stat.ts;
-          row.minSpeed = Math.min(row.minSpeed, speed);
-          row.meanSpeed += speed;
-          row.maxSpeed = Math.max(row.maxSpeed, speed);
-        }
-      }
       for (var i in histo) {
         var row = histo[i];
         var n = row.n;