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 =
|
||||
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 fixedseq, game, simulation, ui
|
||||
import math, random, strformat, strutils, times, std/monotimes
|
||||
import fixedseq, game, simulation
|
||||
|
||||
|
||||
type
|
||||
@ -8,11 +8,22 @@ type
|
||||
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
|
||||
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.write(&" {tr.ops.formatNum} {opname} in {secs.formatNum} seconds\n")
|
||||
stdout.write(&" {(tr.ops.float / secs).formatNum} {opname} per second")
|
||||
stdout.flushFile()
|
||||
|
||||
|
||||
@ -43,43 +54,37 @@ proc newRandomGame(r: var Rand): Board =
|
||||
result.setState(dice, [])
|
||||
|
||||
|
||||
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
|
||||
proc games(nTests, nSamples: Natural, parallel = true) =
|
||||
var r = getRand()
|
||||
var scores: ScoreSet
|
||||
var res: TestResults
|
||||
for i in 1 .. nTests:
|
||||
let b = newRandomGame(r)
|
||||
let dur = executionTime:
|
||||
let s = b.randomGames(nSamples, parallel = parallel)
|
||||
result.ops += s.sum()
|
||||
result.time += dur
|
||||
scores.update(s)
|
||||
res.ops += s.sum()
|
||||
res.time += dur
|
||||
res.summarize("games")
|
||||
|
||||
|
||||
proc testLegs(n: Natural = 100): auto =
|
||||
var boards: seq[Board]
|
||||
var r = initRand(rand(int64))
|
||||
for i in 1 .. n:
|
||||
var b: Board
|
||||
b.init
|
||||
let dice = randomDice(r)
|
||||
b.setState(dice, [])
|
||||
boards.add(b)
|
||||
stdout.write("Constructed: " & $i & "\r")
|
||||
echo ""
|
||||
|
||||
echo "Running..."
|
||||
let start = cpuTime()
|
||||
for b in boards:
|
||||
discard b.getLegScores
|
||||
result = cpuTime() - start
|
||||
proc legs(nTests: Natural) =
|
||||
var r = getRand()
|
||||
var scores: ScoreSet
|
||||
var res: TestResults
|
||||
for i in 1 .. nTests:
|
||||
let b = newRandomGame(r)
|
||||
let dur = executionTime:
|
||||
let s = b.getLegScores
|
||||
scores.update(s)
|
||||
res.ops += s.sum
|
||||
res.time += dur
|
||||
res.summarize("legs")
|
||||
|
||||
|
||||
proc testSpread(nTests, nSamples: Natural) =
|
||||
var b: Board
|
||||
b.init
|
||||
var r = initRand(rand(int64))
|
||||
let dice = randomDice(r)
|
||||
b.setState(dice, [])
|
||||
b.display(1, 5)
|
||||
proc spread(nTests, nSamples: Natural) =
|
||||
var r = getRand()
|
||||
let b = newRandomGame(r)
|
||||
let spread = randomSpread(b, nTests, nSamples)
|
||||
|
||||
stdout.writeLine("Variance:")
|
||||
@ -94,4 +99,4 @@ proc testSpread(nTests, nSamples: Natural) =
|
||||
|
||||
|
||||
when isMainModule:
|
||||
games(10, 10_000_000).summarize()
|
||||
legs(3000)
|
||||
|
Loading…
x
Reference in New Issue
Block a user