From 733cb169900d1d47d01112f774e66d11fd8ba5f2 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 22 Jan 2025 17:23:14 +0000 Subject: timerate: added test illustrating drastic growth of execution time on iteration with quadratic complexity --- tests/cmdMZ.test | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/cmdMZ.test b/tests/cmdMZ.test index 028a71b..d9f4282 100644 --- a/tests/cmdMZ.test +++ b/tests/cmdMZ.test @@ -487,7 +487,26 @@ test cmdMZ-6.12 {Tcl_TimeRateObjCmd: done optimization: nested call of self insi } list [lindex [timerate $m1 1000 5] 2] $x } {5 20} - +test cmdMZ-6.13 {Tcl_TimeRateObjCmd: stability by O(n**2), avoid long execution time on growing iteration time} { + set result {} + # test the function with quadratic complexity (iteration growth 2x, 10x, 100x): + foreach e {2 10 100} { + set x 1 + set m1 [timerate { + apply {x { + while {[incr x -1]} {} + }} [set x [expr {$x*$e}]] + } 50] + lappend result "${e}x" + # check it was too slow (it is OK to use factor 2 to prevent sporadic + # errors on some slow systems or time issues, because if it is not fixed, + # the execution time may grow hundreds and thousand times): + if {[lindex $m1 6] > 50 * 2} { + lappend result "unexpected long: $m1" + } + } + set result +} {2x 10x 100x} # The tests for Tcl_WhileObjCmd are in while.test # cleanup -- cgit v0.12