fix measure time class

This commit is contained in:
Raffaele Mignone 2019-03-13 15:13:15 +01:00
parent fd229c4e39
commit 893058365c
Signed by: norangebit
GPG Key ID: F5255658CB220573
2 changed files with 87 additions and 31 deletions

View File

@ -30,6 +30,7 @@ import org.koin.standalone.KoinComponent
import org.koin.standalone.StandAloneContext.startKoin import org.koin.standalone.StandAloneContext.startKoin
import org.koin.standalone.inject import org.koin.standalone.inject
import java.io.PrintStream import java.io.PrintStream
import kotlin.math.roundToInt
import kotlin.random.Random import kotlin.random.Random
import kotlin.system.measureNanoTime import kotlin.system.measureNanoTime
@ -39,37 +40,64 @@ fun main(args: Array<String>) {
val outEnqueueData = PrintStream(args[0]) val outEnqueueData = PrintStream(args[0])
val outEnDeQueueData = PrintStream(args[1]) val outEnDeQueueData = PrintStream(args[1])
TimeMeasure().run(outEnqueueData, outEnDeQueueData) TimeMeasure(outEnqueueData, outEnDeQueueData)
.run()
} }
val queueModule = module { val queueModule = module {
single { ResizingArrayQueue<Int>() as Queue<Int> } single { ResizingArrayQueue<Int>() as Queue<Int> }
} }
class TimeMeasure : KoinComponent { class TimeMeasure(
private val outEnqueueData: PrintStream,
private val outEnDeQueueData: PrintStream
) : KoinComponent {
private val queue: Queue<Int> by inject() private val queue: Queue<Int> by inject()
fun run(outEnqueueData: PrintStream, outEnDeQueueData: PrintStream) { fun run() {
(0..100000).map { (0 until NUMBER_OF_ITERATION).map {
measureNanoTime { queue.clean()
val numberOfSamples = DEFAULT_SAMPLES *
Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble())
.roundToInt()
val totalTime = measureNanoTime {
repeat(numberOfSamples) {
queue.enqueue(1) queue.enqueue(1)
} }
}
Pair(numberOfSamples, totalTime)
}.forEach { }.forEach {
outEnqueueData.println(it) outEnqueueData.println("${it.first}, ${it.second}")
} }
val random = Random(System.currentTimeMillis()) val random = Random(System.currentTimeMillis())
queue.clean()
(0..100000).map { (0 until NUMBER_OF_ITERATION).map {
measureNanoTime { queue.clean()
val numberOfSamples = DEFAULT_SAMPLES *
Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble())
.roundToInt()
val totalTime = measureNanoTime {
repeat(numberOfSamples) {
if (random.nextBoolean()) if (random.nextBoolean())
queue.enqueue(1) queue.enqueue(1)
else else
queue.dequeue() queue.dequeue()
} }
}
Pair(numberOfSamples, totalTime)
}.forEach { }.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
}
} }

View File

@ -27,9 +27,10 @@ package it.norangeb.algorithms.datastructures.stack
import org.koin.dsl.module.module import org.koin.dsl.module.module
import org.koin.standalone.KoinComponent import org.koin.standalone.KoinComponent
import org.koin.standalone.inject
import org.koin.standalone.StandAloneContext.startKoin import org.koin.standalone.StandAloneContext.startKoin
import org.koin.standalone.inject
import java.io.PrintStream import java.io.PrintStream
import kotlin.math.roundToInt
import kotlin.random.Random import kotlin.random.Random
import kotlin.system.measureNanoTime import kotlin.system.measureNanoTime
@ -39,37 +40,64 @@ fun main(args: Array<String>) {
val outPushData = PrintStream(args[0]) val outPushData = PrintStream(args[0])
val outPushPopData = PrintStream(args[1]) val outPushPopData = PrintStream(args[1])
MeasureTime().run(outPushData, outPushPopData) TimeMeasure(outPushData, outPushPopData)
.run()
} }
val stackModule = module { val stackModule = module {
single { ResizingArrayStack<Int>() as Stack<Int> } single { ResizingArrayStack<Int>() as Stack<Int> }
} }
class MeasureTime : KoinComponent { class TimeMeasure(
private val outPushData: PrintStream,
private val outPushPopData: PrintStream
) : KoinComponent {
private val stack: Stack<Int> by inject() private val stack: Stack<Int> by inject()
fun run(outPushData: PrintStream, outPushPopData: PrintStream) { fun run() {
(0..100000).map { (0 until NUMBER_OF_ITERATION).map {
measureNanoTime { stack.clean()
val numberOfSamples = DEFAULT_SAMPLES *
Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble())
.roundToInt()
val totalTime = measureNanoTime {
repeat(numberOfSamples) {
stack.push(1) stack.push(1)
} }
}
Pair(numberOfSamples, totalTime)
}.forEach { }.forEach {
outPushData.println(it) outPushData.println("${it.first}, ${it.second}")
} }
val random = Random(System.currentTimeMillis()) val random = Random(System.currentTimeMillis())
stack.clean()
(0..100000).map { (0 until NUMBER_OF_ITERATION).map {
measureNanoTime { stack.clean()
val numberOfSamples = DEFAULT_SAMPLES *
Math.pow(GROWTH_FACTOR.toDouble(), it.toDouble())
.roundToInt()
val totalTime = measureNanoTime {
repeat(numberOfSamples) {
if (random.nextBoolean()) if (random.nextBoolean())
stack.push(1) stack.push(1)
else else
stack.pop() stack.pop()
} }
}
Pair(numberOfSamples, totalTime)
}.forEach { }.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
}
} }