more bench improvements
This commit is contained in:
parent
38e9e23dea
commit
e5e90a6ca5
@ -52,7 +52,7 @@ iterator legEndStates(b: Board): Board =
|
|||||||
|
|
||||||
proc getLegScores*(b: Board): ScoreSet =
|
proc getLegScores*(b: Board): ScoreSet =
|
||||||
for prediction in b.legEndStates:
|
for prediction in b.legEndStates:
|
||||||
inc result[prediction.winner.get]
|
inc result[prediction.leader]
|
||||||
|
|
||||||
|
|
||||||
# =====================
|
# =====================
|
||||||
|
71
test.nim
71
test.nim
@ -1,5 +1,5 @@
|
|||||||
import math, random, strformat, times, std/monotimes
|
import math, random, strformat, strutils, times, std/monotimes
|
||||||
import fixedseq, game, simulation, ui
|
import fixedseq, game, simulation
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -8,11 +8,22 @@ type
|
|||||||
time: Duration
|
time: Duration
|
||||||
|
|
||||||
|
|
||||||
proc summarize(tr: TestResults) =
|
proc formatNum(n: SomeNumber, decimals = 0): string =
|
||||||
|
when n is SomeFloat:
|
||||||
|
let s = $n.round(decimals)
|
||||||
|
else:
|
||||||
|
let s = $n
|
||||||
|
var parts = s.split('.')
|
||||||
|
result = parts[0].insertSep(',')
|
||||||
|
if decimals > 0:
|
||||||
|
result = result & '.' & parts[1]
|
||||||
|
|
||||||
|
|
||||||
|
proc summarize(tr: TestResults, opname = "operations") =
|
||||||
let secs = tr.time.inMilliseconds.float / 1000
|
let secs = tr.time.inMilliseconds.float / 1000
|
||||||
stdout.write("Test completed:\n")
|
stdout.write("Test completed:\n")
|
||||||
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
|
stdout.write(&" {tr.ops.formatNum} {opname} in {secs.formatNum} seconds\n")
|
||||||
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
|
stdout.write(&" {(tr.ops.float / secs).formatNum} {opname} per second")
|
||||||
stdout.flushFile()
|
stdout.flushFile()
|
||||||
|
|
||||||
|
|
||||||
@ -43,43 +54,37 @@ proc newRandomGame(r: var Rand): Board =
|
|||||||
result.setState(dice, [])
|
result.setState(dice, [])
|
||||||
|
|
||||||
|
|
||||||
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
|
proc games(nTests, nSamples: Natural, parallel = true) =
|
||||||
var r = getRand()
|
var r = getRand()
|
||||||
var scores: ScoreSet
|
var scores: ScoreSet
|
||||||
|
var res: TestResults
|
||||||
for i in 1 .. nTests:
|
for i in 1 .. nTests:
|
||||||
let b = newRandomGame(r)
|
let b = newRandomGame(r)
|
||||||
let dur = executionTime:
|
let dur = executionTime:
|
||||||
let s = b.randomGames(nSamples, parallel = parallel)
|
let s = b.randomGames(nSamples, parallel = parallel)
|
||||||
result.ops += s.sum()
|
scores.update(s)
|
||||||
result.time += dur
|
res.ops += s.sum()
|
||||||
|
res.time += dur
|
||||||
|
res.summarize("games")
|
||||||
|
|
||||||
|
|
||||||
proc testLegs(n: Natural = 100): auto =
|
proc legs(nTests: Natural) =
|
||||||
var boards: seq[Board]
|
var r = getRand()
|
||||||
var r = initRand(rand(int64))
|
var scores: ScoreSet
|
||||||
for i in 1 .. n:
|
var res: TestResults
|
||||||
var b: Board
|
for i in 1 .. nTests:
|
||||||
b.init
|
let b = newRandomGame(r)
|
||||||
let dice = randomDice(r)
|
let dur = executionTime:
|
||||||
b.setState(dice, [])
|
let s = b.getLegScores
|
||||||
boards.add(b)
|
scores.update(s)
|
||||||
stdout.write("Constructed: " & $i & "\r")
|
res.ops += s.sum
|
||||||
echo ""
|
res.time += dur
|
||||||
|
res.summarize("legs")
|
||||||
echo "Running..."
|
|
||||||
let start = cpuTime()
|
|
||||||
for b in boards:
|
|
||||||
discard b.getLegScores
|
|
||||||
result = cpuTime() - start
|
|
||||||
|
|
||||||
|
|
||||||
proc testSpread(nTests, nSamples: Natural) =
|
proc spread(nTests, nSamples: Natural) =
|
||||||
var b: Board
|
var r = getRand()
|
||||||
b.init
|
let b = newRandomGame(r)
|
||||||
var r = initRand(rand(int64))
|
|
||||||
let dice = randomDice(r)
|
|
||||||
b.setState(dice, [])
|
|
||||||
b.display(1, 5)
|
|
||||||
let spread = randomSpread(b, nTests, nSamples)
|
let spread = randomSpread(b, nTests, nSamples)
|
||||||
|
|
||||||
stdout.writeLine("Variance:")
|
stdout.writeLine("Variance:")
|
||||||
@ -94,4 +99,4 @@ proc testSpread(nTests, nSamples: Natural) =
|
|||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
games(10, 10_000_000).summarize()
|
legs(3000)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user