Skip to content

Instantly share code, notes, and snippets.

@Araq
Araq / bintrees.nim
Created May 8, 2023 06:12
Binary trees benchmark
include prelude
type
Node {.acyclic.} = ref object
le, ri: owned Node
proc checkTree(n: Node): int =
if n.le == nil: 1
else: 1 + checkTree(n.le) + checkTree(n.ri)
discard """
cmd: "nim c --gc:orc $file"
output: '''
{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
true
true
alpha 100
omega 200
0'''
"""
import asynchttpserver, asyncdispatch, strutils, json, tables, streams
# about 130 MB of alive data:
var sessions: Table[string, JsonNode]
for i in 0..<10:
sessions[$i] = parseJson(newFileStream("1.json", fmRead), "1.json")
var served = 0
import macros
macro awaitE(call: typed, r: untyped): untyped =
let x = call[0]
let tx = x.getType()
expectKind tx, nnkBracketExpr
var t = newTree(nnkProcTy)
var p = newTree(nnkFormalParams, tx[1])
import strutils, os
type Target = tuple[repo, package, ext: string]
proc newTarget*(path: string): Target =
let splat = path.splitFile
result = (repo: splat.dir, package: splat.name, ext: splat.ext)
let mem = getOccupiedMem()
const
intrin = "<x86intrin.h>"
{.localPassC: "-msse4.2".}
type
M128i {.importc: "__m128i", header: intrin, bycopy.} = object
const
SIDD_CMP_RANGES = 0b0000_0100'i32
SIDD_NEGATIVE_POLARITY = 0b0001_0000'i32
proc complement(s: set[char]): string =
var last = '\0'
result = ""
for elem in '\0'..'\255':
if elem notin s:
if result.len == 0:
result.add elem
result.add elem
elif pred(elem) == last:
result[^1] = elem # patch last interval
{.push staticBoundChecks: on.}
proc takeNat(n: Natural) =
discard
proc p(a: openArray[int]) =
if a.len > 0:
echo a[0]
include prelude
type
Node {.acyclic.} = ref object
le, ri: Node
proc checkTree(n: Node): int =
if n.le == nil: 1
else: 1 + checkTree(n.le) + checkTree(n.ri)
# Compile and run with 'nim c -r -d:useRealtimeGC -d:release main.nim'
import strutils
#import times
include "lib/system/timers"
const
windowSize = 200000
msgCount = 1000000