Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created April 13, 2020 19:53
Show Gist options
  • Save Varriount/d7c3961f95870a1c6520523131c3971e to your computer and use it in GitHub Desktop.
Save Varriount/d7c3961f95870a1c6520523131c3971e to your computer and use it in GitHub Desktop.
Note: Requires the `criterion` Nimble package.
import criterion
var cfg = newDefaultConfig()
cfg.brief = true
const
sepSet = {'/', '\\'}
sepArray = ['/', '\\']
proc dummy(a: string): bool =
return true
proc control(a: string): bool =
return true
proc controlFor(a: string): bool =
for character in a:
result = true
proc hasSepSetFor(a: string): bool =
for character in a:
if character in sepSet:
return true
return false
proc hasSepArrayFor(a: string): bool =
for character in a:
if character in sepArray:
return true
return false
proc hasSepInlineFor(a: string): bool =
for character in a:
if character == '/' or character == '\\':
return true
return false
# ----
proc controlWhile(a: string): bool =
let limit = high(a)
var index = 0
while index <= limit:
result = true
index += 1
proc hasSepSetWhile(a: string): bool =
let limit = high(a)
var index = 0
while index <= limit:
let character = a[index]
if character in sepSet:
return true
index += 1
return false
proc hasSepArrayWhile(a: string): bool =
let limit = high(a)
var index = 0
while index <= limit:
let character = a[index]
if character in sepArray:
return true
index += 1
return false
proc hasSepInlineWhile(a: string): bool =
let limit = high(a)
var index = 0
while index <= limit:
let character = a[index]
if character == '/' or character == '\\':
return true
index += 1
return false
proc `$`(p: proc (a: string): bool{.noSideEffect, gcsafe, locks: 0.}): string =
if p == dummy:
return "dummy"
if p == control:
return "control"
if p == controlFor:
return "controlFor"
if p == hasSepSetFor:
return "hasSepSetFor"
if p == hasSepArrayFor:
return "hasSepArrayFor"
if p == hasSepInlineFor:
return "hasSepInlineFor"
if p == controlWhile:
return "controlWhile"
if p == hasSepSetWhile:
return "hasSepSetWhile"
if p == hasSepArrayWhile:
return "hasSepArrayWhile"
if p == hasSepInlineWhile:
return "hasSepInlineWhile"
else:
return "Unknown Function"
benchmark cfg:
iterator args(): auto =
const paths = [
"C-__Users_JohnSomeone_Documents/",
"C-__Users_JohnSomeone_Documents\\",
"C-__Users_JohnSomeone_Documents",
]
const procs = [
hasSepInlineWhile,
hasSepArrayWhile,
hasSepSetWhile,
controlWhile,
hasSepInlineFor,
hasSepArrayFor,
hasSepSetFor,
controlFor,
control,
dummy,
]
for path in paths:
for procc in procs:
yield (path, procc)
proc hasSep(a: string, b: auto) {.measure: args.} =
blackBox b(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment