diff --git a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/MeasureTime.kt b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/MeasureTime.kt index 1b00dda..1afd22f 100644 --- a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/MeasureTime.kt +++ b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/MeasureTime.kt @@ -30,6 +30,7 @@ import org.koin.standalone.KoinComponent import org.koin.standalone.StandAloneContext.startKoin import org.koin.standalone.inject import java.io.PrintStream +import kotlin.math.roundToInt import kotlin.random.Random import kotlin.system.measureNanoTime @@ -39,37 +40,64 @@ fun main(args: Array) { val outEnqueueData = PrintStream(args[0]) val outEnDeQueueData = PrintStream(args[1]) - TimeMeasure().run(outEnqueueData, outEnDeQueueData) + TimeMeasure(outEnqueueData, outEnDeQueueData) + .run() } val queueModule = module { single { ResizingArrayQueue() as Queue } } -class TimeMeasure : KoinComponent { +class TimeMeasure( + private val outEnqueueData: PrintStream, + private val outEnDeQueueData: PrintStream +) : KoinComponent { private val queue: Queue by inject() - fun run(outEnqueueData: PrintStream, outEnDeQueueData: PrintStream) { - (0..100000).map { - measureNanoTime { - queue.enqueue(1) + fun run() { + (0 until NUMBER_OF_ITERATION).map { + queue.clean() + val numberOfSamples = DEFAULT_SAMPLES * + Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble()) + .roundToInt() + + val totalTime = measureNanoTime { + repeat(numberOfSamples) { + queue.enqueue(1) + } } + + Pair(numberOfSamples, totalTime) }.forEach { - outEnqueueData.println(it) + outEnqueueData.println("${it.first}, ${it.second}") } val random = Random(System.currentTimeMillis()) - queue.clean() - (0..100000).map { - measureNanoTime { - if (random.nextBoolean()) - queue.enqueue(1) - else - queue.dequeue() + (0 until NUMBER_OF_ITERATION).map { + queue.clean() + val numberOfSamples = DEFAULT_SAMPLES * + Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble()) + .roundToInt() + + val totalTime = measureNanoTime { + repeat(numberOfSamples) { + if (random.nextBoolean()) + queue.enqueue(1) + else + queue.dequeue() + } } + + Pair(numberOfSamples, totalTime) }.forEach { - outEnDeQueueData.println(it) + outEnDeQueueData.println("${it.first}, ${it.second}") } } + + companion object { + const val DEFAULT_SAMPLES = 1000 + const val NUMBER_OF_ITERATION = 15 + const val GROWTH_FACTOR = 2 + } } \ No newline at end of file diff --git a/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/MeasureTime.kt b/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/MeasureTime.kt index dc0cc2f..42938a0 100644 --- a/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/MeasureTime.kt +++ b/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/MeasureTime.kt @@ -27,9 +27,10 @@ package it.norangeb.algorithms.datastructures.stack import org.koin.dsl.module.module import org.koin.standalone.KoinComponent -import org.koin.standalone.inject import org.koin.standalone.StandAloneContext.startKoin +import org.koin.standalone.inject import java.io.PrintStream +import kotlin.math.roundToInt import kotlin.random.Random import kotlin.system.measureNanoTime @@ -39,37 +40,64 @@ fun main(args: Array) { val outPushData = PrintStream(args[0]) val outPushPopData = PrintStream(args[1]) - MeasureTime().run(outPushData, outPushPopData) + TimeMeasure(outPushData, outPushPopData) + .run() } val stackModule = module { single { ResizingArrayStack() as Stack } } -class MeasureTime : KoinComponent { +class TimeMeasure( + private val outPushData: PrintStream, + private val outPushPopData: PrintStream +) : KoinComponent { private val stack: Stack by inject() - fun run(outPushData: PrintStream, outPushPopData: PrintStream) { - (0..100000).map { - measureNanoTime { - stack.push(1) + fun run() { + (0 until NUMBER_OF_ITERATION).map { + stack.clean() + val numberOfSamples = DEFAULT_SAMPLES * + Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble()) + .roundToInt() + + val totalTime = measureNanoTime { + repeat(numberOfSamples) { + stack.push(1) + } } + + Pair(numberOfSamples, totalTime) }.forEach { - outPushData.println(it) + outPushData.println("${it.first}, ${it.second}") } val random = Random(System.currentTimeMillis()) - stack.clean() - (0..100000).map { - measureNanoTime { - if (random.nextBoolean()) - stack.push(1) - else - stack.pop() + (0 until NUMBER_OF_ITERATION).map { + stack.clean() + val numberOfSamples = DEFAULT_SAMPLES * + Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble()) + .roundToInt() + + val totalTime = measureNanoTime { + repeat(numberOfSamples) { + if (random.nextBoolean()) + stack.push(1) + else + stack.pop() + } } + + Pair(numberOfSamples, totalTime) }.forEach { - outPushPopData.println(it) + outPushPopData.println("${it.first}, ${it.second}") } } + + companion object { + const val DEFAULT_SAMPLES = 1000 + const val NUMBER_OF_ITERATION = 15 + const val GROWTH_FACTOR = 2 + } } \ No newline at end of file