Skip to content

Instantly share code, notes, and snippets.

@southp
Created April 22, 2016 08:43
Show Gist options
  • Save southp/24ee9d54a6e0c4fb4f806f3cfb92ccb9 to your computer and use it in GitHub Desktop.
Save southp/24ee9d54a6e0c4fb4f806f3cfb92ccb9 to your computer and use it in GitHub Desktop.
"use strict";
const toMs = ( beg, end ) => {
const sec = end[0] - beg[0];
const ms = Math.floor( (end[1] - beg[1]) / 1000 );
return sec * 1000 + ms;
};
const SIZE = 1000000;
let arr = [];
for ( var i = 0; i < SIZE; ++i ) {
arr.push( Math.floor( Math.random() * 256 ) );
}
let prev = process.hrtime();
let sum = 0;
for ( var i = 0; i < SIZE; ++i ) {
const val = arr[ i ] ;
if ( val > 128 ) {
sum += val;
}
}
let t1 = toMs( prev, process.hrtime() );
console.log( "Time cost: ", t1, "ms", sum );
arr.sort();
prev = process.hrtime();
sum = 0;
for ( var i = 0; i < SIZE; ++i ) {
const val = arr[ i ] ;
if ( val > 128 ) {
sum += val;
}
}
let t2 = toMs( prev, process.hrtime() );
console.log( "Time cost: ", t2, "ms", sum );
console.log( "Ratio t2/t1: ", t2 / t1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment