Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Created April 11, 2017 14:32
Show Gist options
  • Save JobLeonard/80b894adfe3e635ef03e7f6ebc55d086 to your computer and use it in GitHub Desktop.
Save JobLeonard/80b894adfe3e635ef03e7f6ebc55d086 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Filling indices</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
let indices = new Uint32Array(100000);
};
suite.add("for loop", function () {
// for loop
for (let i = 0; i < indices.length; i++){
indices[i] = i;
}
});
suite.add("decrementing while", function () {
// decrementing while
let i = indices.length;
while (i--){
indices[i] = i;
}
});
suite.add("unrolled 8 - offset", function () {
// unrolled 8 - offset
let i = 0;
while(i+8 < indices.length){
indices[i] = i;
indices[i+1] = i+1;
indices[i+2] = i+2;
indices[i+3] = i+3;
indices[i+4] = i+4;
indices[i+5] = i+5;
indices[i+6] = i+6;
indices[i+7] = i+7;
i += 8;
}
while(i < indices.length){
indices[i] = i++;
}
});
suite.add("unrolled 8 - increment", function () {
// unrolled 8 - increment
let i = 0;
while(i+8 < indices.length){
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
}
while(i < indices.length){
indices[i] = i++;
}
});
suite.add("unrolled 8 - decrement", function () {
// unrolled 8 - decrement
let i = indices.length;
while(i-8 > 0){
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
}
while(i--){
indices[i] = i;
}
});
suite.add("unrolled 16 - increment", function () {
// unrolled 16 - increment
let i = 0;
while(i+16 < indices.length){
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
indices[i] = i++;
}
while(i < indices.length){
indices[i] = i++;
}
});
suite.add("unrolled 16 - decrement", function () {
// unrolled 16 - decrement
let i = indices.length;
while(i-16 > 0){
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
indices[--i] = i;
}
while(i--){
indices[i] = i;
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Filling indices");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment