diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..89af941 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +test : + node speci.js x-first + node speci.js y-first + node speci.js mixed + node speci.js rand-suffix + node speci.js rand-prefix + diff --git a/readme.md b/readme.md index 81dd0d4..d2c0880 100644 --- a/readme.md +++ b/readme.md @@ -9,44 +9,59 @@ handle a couple of different variants -- but this might not be true of all possibilities. 256 randomized variants is linearly slower. ``` - % node speci.js -** x_first +% make test +node speci.js x-first +** x-first run 1 -72 ms +189 ms run 2 -15 ms +293 ms run 3 -22 ms +178 ms -** y_first +node speci.js y-first +** y-first run 1 -73 ms +196 ms run 2 -23 ms +175 ms run 3 -20 ms +174 ms +node speci.js mixed ** mixed run 1 -2766 ms +732 ms run 2 -52 ms +716 ms run 3 -53 ms +912 ms -** randomized +node speci.js rand-suffix +** rand-suffix run 1 -464 ms +15596 ms run 2 -445 ms +15636 ms run 3 -445 ms +15501 ms + +node speci.js rand-prefix +** rand-prefix +run 1 +18896 ms + +run 2 +18883 ms + +run 3 +18784 ms ``` diff --git a/speci.js b/speci.js index 3b31dcf..8cf3b0d 100644 --- a/speci.js +++ b/speci.js @@ -1,4 +1,4 @@ -function doit() { +function doit(mode) { function make_x_first(x,y) { return {x, y}; @@ -16,23 +16,23 @@ function doit() { } } - function make_randomized(x, y) { + function make_rand_prefix(x, y) { let rando = (Math.random() * (2 ** 32 - 1)) | 0; - let buckets = 256; - let r = 'random' + (rando % buckets); - let o = {[r]: r}; - if (Math.random() > 0.5) { - o.y = y; - o.x = x; - } else { - o.x = x; - o.y = y; - } + let r = 'random' + rando; + let o = {[r]: r, x, y}; + return o; + } + + function make_rand_suffix(x, y) { + let rando = (Math.random() * (2 ** 32 - 1)) | 0; + let r = 'random' + rando; + let o = {x, y, [r]: r}; return o; } const million = 1000 * 1000; - const n = 10 * million; + const n = 1 * million; + const runs = 100 * n; function bulk(filler) { let a = []; @@ -42,10 +42,22 @@ function doit() { return a; } - const x_first = bulk(make_x_first); - const y_first = bulk(make_y_first); - const mixed = bulk(make_mixed); - const randomized = bulk(make_randomized); + let funcMap = { + 'x-first': make_x_first, + 'y-first': make_y_first, + 'mixed': make_mixed, + 'rand-prefix': make_rand_prefix, + 'rand-suffix': make_rand_suffix, + }; + let func = funcMap[mode]; + if (!func) { + console.log(`Pass as parameter one of:`); + for (let key of Object.keys(funcMap)) { + console.log(` ${key}`); + } + process.exit(1); + } + const data = bulk(func); function time(func) { const start = Date.now(); @@ -64,8 +76,8 @@ function doit() { console.log(`run ${i+1}`); report(() => { let sumX = 0, sumY = 0; - for (let i = 0; i < dataSet.length; i++) { - let item = dataSet[i]; + for (let i = 0; i < runs; i++) { + let item = dataSet[i % dataSet.length]; sumX += item.x; sumY += item.y; } @@ -75,18 +87,9 @@ function doit() { } } - console.log(`** x_first`); - demo(x_first); - - console.log(`** y_first`); - demo(y_first); - - console.log(`** mixed`); - demo(mixed); - - console.log(`** randomized`); - demo(randomized); - + console.log(`** ${mode}`); + demo(data); } -doit(); \ No newline at end of file +let mode = String(process.argv[2]); +doit(mode); \ No newline at end of file