Created
April 22, 2016 08:43
-
-
Save southp/24ee9d54a6e0c4fb4f806f3cfb92ccb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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