From: Jérôme Benoit Date: Sun, 23 Oct 2022 21:08:04 +0000 (+0200) Subject: Improve random integer array generation X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ed40d2b012a740d121f1951b9be91df341d6eaf3;p=benchmarks-js.git Improve random integer array generation Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.js b/benchmark-utils.js index 56689d6..1e7914f 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -29,6 +29,19 @@ function generateRandomInteger (max, min = 0) { return Math.floor(secureRandom() * (max + 1)) } +/** + * + * @param size + * @param max + */ +function generateRandomIntegerArray (size, max = Number.MAX_SAFE_INTEGER) { + const integerArray = [] + for (let i = 0; i < size; i++) { + integerArray.push(generateRandomInteger(max)) + } + return integerArray +} + /** * @param ms * @returns @@ -42,4 +55,10 @@ const LIST_FORMATTER = new Intl.ListFormat('en-US', { type: 'conjunction' }) -module.exports = { generateRandomInteger, sleep, secureRandom, LIST_FORMATTER } +module.exports = { + generateRandomInteger, + generateRandomIntegerArray, + sleep, + secureRandom, + LIST_FORMATTER +} diff --git a/busy-wait.js b/busy-wait.js index 46e5853..eece669 100644 --- a/busy-wait.js +++ b/busy-wait.js @@ -61,7 +61,7 @@ suite dummyTimeoutBusyWait(timeout) }) .add('sleepTimeoutBusyWait', async () => { - sleepTimeoutBusyWait(timeout) + await sleepTimeoutBusyWait(timeout) }) .add('divideAndConquerTimeoutBusyWait', async () => { await divideAndConquerTimeoutBusyWait(timeout) diff --git a/empty-array.js b/empty-array.js index e623b5e..9ddc17d 100644 --- a/empty-array.js +++ b/empty-array.js @@ -1,19 +1,7 @@ const Benchmark = require('benny') +const { generateRandomIntegerArray } = require('./benchmark-utils') -let testArray = [ - 83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, - 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, - 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, - 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, - 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, - 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, - 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, - 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, - 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, - 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, - 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, - 36, 28 -] +let testArray = generateRandomIntegerArray(10000) Benchmark.suite( 'Empty array', diff --git a/max.js b/max.js index d0d1381..5525c92 100644 --- a/max.js +++ b/max.js @@ -1,19 +1,7 @@ const Benchmark = require('benny') +const { generateRandomIntegerArray } = require('./benchmark-utils') -const testArray = [ - 83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, - 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, - 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, - 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, - 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, - 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, - 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, - 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, - 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, - 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, - 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, - 36, 28 -] +const testArray = generateRandomIntegerArray(10000) /** *