From 28de77dc7975a9f06631705a5a11cdf925d96e5c Mon Sep 17 00:00:00 2001 From: tursom Date: Tue, 17 Aug 2021 14:28:28 +0800 Subject: [PATCH] update FlowGraphic --- ts-core/src/test/kotlin/FlowExample.kt | 67 ++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/ts-core/src/test/kotlin/FlowExample.kt b/ts-core/src/test/kotlin/FlowExample.kt index 0934794..5f08ac8 100644 --- a/ts-core/src/test/kotlin/FlowExample.kt +++ b/ts-core/src/test/kotlin/FlowExample.kt @@ -1,3 +1,4 @@ +import cn.tursom.core.usingNanoTime import org.junit.Test data class Flow( @@ -61,6 +62,13 @@ class Edge { return false } + fun clear() { + repeat(2) { + flow[it] = 0 + flowCache[it] = 0 + } + } + override fun toString(): String { return "Edge(flow=${flow.contentToString()}, flowCache=${flowCache.contentToString()})" } @@ -75,6 +83,9 @@ class Graphic( ) = GraphicBuilder().also(builder).build() } + @DslMarker + annotation class Builder + private val edgeList = ArrayList() var input: Int = 0 var inputFlowChannel: FlowChannel = FlowChannel.A @@ -109,12 +120,19 @@ class Graphic( } } + fun clear() { + edgeList.forEach { edge -> + edge.clear() + } + } + fun result(): List> { return edgeList.map { it[FlowChannel.A] to it[FlowChannel.B] } } override fun iterator(): Iterator = edgeList.iterator() + @Builder class GraphicBuilder { var precision: Int = 8 val edgeMap = ArrayList() @@ -134,6 +152,7 @@ class Graphic( } } + @Builder class EdgeBuilder(private val id: Int) { private val flowList = ArrayList() fun init(graphic: Graphic) { @@ -156,6 +175,7 @@ class Graphic( } } + @Builder data class FlowBuilder( val source: Int, val sourceFlowChannel: Int, @@ -171,17 +191,58 @@ class FlowExample { graphic.precision = 8 var changed = true - var it = 0 + var step = 0 while (changed) { - it++ + step++ graphic.calc() changed = graphic.changed() - println("step $it changed $changed") + println("step $step changed $changed") graphic.finishCalc() println(graphic.result()) } } + /** + * 性能测试 + */ + @Test + fun testPerformance() { + val graphic = getTestGraphic() + + // 热车 + graphic.precision = 30 + repeat(1000) { + graphic.clear() + var changed = true + while (changed) { + graphic.calc() + changed = graphic.changed() + graphic.finishCalc() + } + } + + // 测试 + repeat(5) { + intArrayOf(8, 12, 16, 20, 24, 28).forEach { precision -> + graphic.clear() + graphic.precision = precision + + var step = 0 + var changed = true + val usingTime = usingNanoTime { + while (changed) { + step++ + graphic.calc() + changed = graphic.changed() + graphic.finishCalc() + } + } + + println("precision $precision step $step using $usingTime us") + } + } + } + fun getTestGraphic() = Graphic { // 0 edge {