Skip to content

Instantly share code, notes, and snippets.

@planetis-m
Created December 24, 2020 12:10
Show Gist options
  • Save planetis-m/fa12f6c4ff528babf9f15f3d189038de to your computer and use it in GitHub Desktop.
Save planetis-m/fa12f6c4ff528babf9f15f3d189038de to your computer and use it in GitHub Desktop.
import tables
type
DirKind = enum # must be ordered alphabetically!
dkNone, dkAttention, dkAuthor, dkAuthors, dkCaution, dkCode, dkCodeBlock, dkContainer, dkContents, dkDanger,
dkError, dkFigure, dkHint, dkImage, dkImportant, dkInclude, dkIndex, dkNote, dkRaw, dkTip, dkTitle, dkWarning
const
DirIds: array[DirKind, string] = [
dkNone: "",
dkAttention: "attention",
dkAuthor: "author",
dkAuthors: "authors",
dkCaution: "caution",
dkCode: "code",
dkCodeBlock: "code-block",
dkContainer: "container",
dkContents: "contents",
dkDanger: "danger",
dkError: "error",
dkFigure: "figure",
dkHint: "hint",
dkImage: "image",
dkImportant: "important",
dkInclude: "include",
dkIndex: "index",
dkNote: "note",
dkRaw: "raw",
dkTip: "tip",
dkTitle: "title",
dkWarning: "warning"]
proc getDirKind(s: string): DirKind =
let i = find(DirIds, s)
if i >= 0: result = DirKind(i)
else: result = dkNone
proc dirUnimpl(d: string): string =
result = d
var directives: Table[string, proc(d: string): string]
for a in ["code", "code-block", "container", "author", "authors", "contents", "figure",
"image", "include", "index", "raw", "title", "admonition",
"attention", "caution", "danger", "error", "hint",
"important", "note", "tip", "warning"]:
directives[a] = dirUnimpl
import times, stats, strformat
proc printStats(name: string, stats: RunningStat, dur: float) =
echo &"""{name}:
Collected {stats.n} samples in {dur:.4} seconds
Average time: {stats.mean * 1000:.4} ms
Stddev time: {stats.standardDeviationS * 1000:.4} ms
Min time: {stats.min * 1000:.4} ms
Max time: {stats.max * 1000:.4} ms"""
proc testEnums(d: string): string =
case getDirKind(d)
of dkInclude: result = dirUnimpl(d)
of dkImage: result = dirUnimpl(d)
of dkFigure: result = dirUnimpl(d)
of dkTitle: result = dirUnimpl(d)
of dkContainer: result = dirUnimpl(d)
of dkContents: result = dirUnimpl(d)
of dkRaw: result = dirUnimpl(d)
of dkCode: result = dirUnimpl(d)
of dkCodeBlock: result = dirUnimpl(d)
of dkIndex: result = dirUnimpl(d)
of dkAuthor: result = dirUnimpl(d)
of dkAuthors: result = dirUnimpl(d)
else:
assert false
proc testTable(d: string): string =
if d in directives: result = directives[d](d)
else: assert false
proc warmup() =
# Warmup - make sure cpu is on max perf
let start = cpuTime()
var a = 123
for i in 0 ..< 300_000_000:
a += i * i mod 456
a = a mod 789
let dur = cpuTime() - start
echo &"Warmup: {dur:>4.4f} s ", a
proc test1 =
let data = @["code", "code-block", "author", "authors", "code", "image", "title", "raw", "container", "raw"]
var stats: RunningStat
let globalStart = cpuTime()
var len = 0
for i in 1..10000:
let start = cpuTime()
for s in data: len.inc testEnums(s).len
let duration = cpuTime() - start
stats.push duration
let globalDuration = cpuTime() - globalStart
echo len
printStats("Run update", stats, globalDuration)
proc test2 =
let data = @["code", "code-block", "author", "authors", "code", "image", "title", "raw", "container", "raw"]
var stats: RunningStat
let globalStart = cpuTime()
var len = 0
for i in 1..10000:
let start = cpuTime()
for s in data: len.inc testTable(s).len
let duration = cpuTime() - start
stats.push duration
let globalDuration = cpuTime() - globalStart
echo len
printStats("Run update", stats, globalDuration)
warmup()
test1()
test2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment