Skip to content

Instantly share code, notes, and snippets.

@kurtroberts
Created October 13, 2015 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtroberts/221ac14eb2a9776053e8 to your computer and use it in GitHub Desktop.
Save kurtroberts/221ac14eb2a9776053e8 to your computer and use it in GitHub Desktop.
I really thought at the level of abstraction we typically work (Node.js) the idea of "write combining" wasn't going to make a difference. I was wrong.
//To understand how I started down this rabbit hole, go here: http://mechanical-sympathy.blogspot.com/2011/07/write-combining.html
var iterations = parseInt(2147483647 / 128);
//Base case, no optimization
function runCaseOne () {
console.time('caseOne');
var i = iterations,
arrayA = [],
arrayB = [],
arrayC = [],
arrayD = [],
arrayE = [],
arrayF = [];
while (--i > 0) {
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
}
console.timeEnd('caseOne');
}
//Write combining
function runCaseTwo () {
console.time('caseTwo');
var i = iterations,
arrayA = [],
arrayB = [],
arrayC = [],
arrayD = [],
arrayE = [],
arrayF = [];
while (--i > 0) {
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
}
i = iterations;
while (--i > 0) {
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
}
console.timeEnd('caseTwo');
}
//Loop unrolling
function runCaseThree () {
console.time('caseThree');
var i = iterations,
arrayA = [],
arrayB = [],
arrayC = [],
arrayD = [],
arrayE = [],
arrayF = [];
while (i > 0) {
i = i - 4;
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
}
console.timeEnd('caseThree');
}
//Loop unrolled write combining?
function runCaseFour () {
console.time('caseFour');
var i = iterations,
arrayA = [],
arrayB = [],
arrayC = [],
arrayD = [],
arrayE = [],
arrayF = [];
while (i > 0) {
i = i - 4;
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
arrayA.push(1);
arrayB.push(1);
arrayC.push(1);
}
i = iterations;
while (i > 0) {
i = i - 4;
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
arrayD.push(1);
arrayE.push(1);
arrayF.push(1);
}
console.timeEnd('caseFour');
}
for (var i = 1; i <=3; i++) {
runCaseOne();
runCaseTwo();
runCaseThree();
runCaseFour();
}
@kurtroberts
Copy link
Author

kroberts-macpro:Desktop kroberts$ node looptest.js 
caseOne: 4668ms
caseTwo: 4169ms
caseThree: 4667ms
caseFour: 3538ms
caseOne: 4448ms
caseTwo: 3008ms
caseThree: 4271ms
caseFour: 3102ms
caseOne: 4287ms
caseTwo: 3243ms
caseThree: 4343ms
caseFour: 2834ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment