Compare commits
1 Commits
cade17a9a6
...
2687bd2198
Author | SHA1 | Date | |
---|---|---|---|
2687bd2198 |
70
test.nim
70
test.nim
@ -1,32 +1,7 @@
|
|||||||
import math, random, strformat, times, std/monotimes
|
import math, random, strformat, times
|
||||||
import fixedseq, game, simulation, ui
|
import fixedseq, game, simulation, ui
|
||||||
|
|
||||||
|
|
||||||
type
|
|
||||||
TestResults = object
|
|
||||||
ops: int
|
|
||||||
time: Duration
|
|
||||||
|
|
||||||
|
|
||||||
proc summarize(tr: TestResults) =
|
|
||||||
let secs = tr.time.inMilliseconds.float / 1000
|
|
||||||
stdout.write("Test completed:\n")
|
|
||||||
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
|
|
||||||
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
|
|
||||||
stdout.flushFile()
|
|
||||||
|
|
||||||
|
|
||||||
template executionTime(body: untyped): Duration =
|
|
||||||
let start = getMonoTime()
|
|
||||||
body
|
|
||||||
getMonoTime() - start
|
|
||||||
|
|
||||||
|
|
||||||
proc getRand(): Rand =
|
|
||||||
randomize()
|
|
||||||
result = initRand(rand(int64))
|
|
||||||
|
|
||||||
|
|
||||||
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
||||||
for c in Color:
|
for c in Color:
|
||||||
let v = r.rand(1..3)
|
let v = r.rand(1..3)
|
||||||
@ -43,15 +18,18 @@ proc newRandomGame(r: var Rand): Board =
|
|||||||
result.setState(dice, [])
|
result.setState(dice, [])
|
||||||
|
|
||||||
|
|
||||||
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
|
proc testGames(n: SomeInteger = 100): auto =
|
||||||
var r = getRand()
|
var r = initRand(rand(int64))
|
||||||
var scores: ScoreSet
|
let dice = randomDice(r)
|
||||||
for i in 1 .. nTests:
|
var b: Board
|
||||||
let b = newRandomGame(r)
|
b.init
|
||||||
let dur = executionTime:
|
b.setState(dice, [])
|
||||||
let s = b.randomGames(nSamples, parallel = parallel)
|
b.display(1, 5)
|
||||||
result.ops += s.sum()
|
|
||||||
result.time += dur
|
let startTime = cpuTime()
|
||||||
|
let scores = b.randomGames(n)
|
||||||
|
result = cpuTime() - startTime
|
||||||
|
scores.display()
|
||||||
|
|
||||||
|
|
||||||
proc testLegs(n: Natural = 100): auto =
|
proc testLegs(n: Natural = 100): auto =
|
||||||
@ -94,4 +72,24 @@ proc testSpread(nTests, nSamples: Natural) =
|
|||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
games(10, 10_000_000).summarize()
|
randomize()
|
||||||
|
var r = initRand(rand(int64))
|
||||||
|
let b = newRandomGame(r)
|
||||||
|
b.display(1, 5)
|
||||||
|
echo b.showSpaces(1, 16)
|
||||||
|
|
||||||
|
let scores = b.getLegScores
|
||||||
|
echo scores.showPercents
|
||||||
|
# let start_states = 2_000
|
||||||
|
# let executionTime = testLegs(start_states)
|
||||||
|
# echo "Execution time: ", executionTime
|
||||||
|
# echo "Leg simulations per second: ", float(start_states * 29_160) / executionTime
|
||||||
|
|
||||||
|
# for i in 1 .. 1:
|
||||||
|
# let num_games = 100_000_005
|
||||||
|
# let executionTime = testGames(num_games)
|
||||||
|
# echo "Execution time: ", executionTime
|
||||||
|
# echo "Full-game simulations per second: ", float(num_games) / executionTime
|
||||||
|
# echo ""
|
||||||
|
|
||||||
|
# testSpread(100, 1_000_000)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user