// Find in a 4.5 MB corpus, StringZilla against `std/string`'s contains // (a memmem-style two-way search in the runtime): needle lengths 1, 4, // 16 and 64, found near the end or absent; then a 10k-string sort // against `list.sort`. Not a test: build it with `louise build // --release` in this directory and run `build/stringzilla_bench`. Each // line is the time per call; the number in brackets adds up what the // calls gave back, so the work cannot be skipped. // // `string.contains` answers a Bool where `stringzilla.find` answers the // offset: same scan, a trivially cheaper result. WS-A's SIMD // `string.find` prim (issue gabla108/louise#380) had not landed when this was written; // add its line here when it has. import std/io import std/list import std/option import std/string import std/time.{type clock} import stringzilla // Calls per measurement. fn rounds :: -> Int { 50 } fn per_call :: label: String work: fn(Int) -> Int / -> () / { let start = time.now () let total = list.fold (list.range 1 (rounds ())) 0 fn acc i { acc + work i } let ns = time.in_nanoseconds (time.since (time.now ()) start) / rounds () io.println "{label}: {ns} ns per call ({total})" } /// The offset of `needle`, or 0: the work `find` does, as an Int. fn at :: corpus: String needle: String -> Int / { option.unwrap_or (stringzilla.find corpus needle) 0 } pub fn main :: -> () / { io.println "stringzilla {stringzilla.version ()} ({stringzilla.capabilities ()})" // 45 bytes × 100,000 = 4.5 MB; the hit needles occur once, at the end. let chunk = "the quick brown fox jumps over the lazy dog. " let corpus = string.repeat chunk 100000 <> "one buried landmark: xylophone" let hit64 = string.slice corpus (string.length corpus - 64) (string.length corpus) list.each [ ("1 byte", "x", "1") , ("4 bytes", "land", "zzzz") , ("16 bytes", "buried landmark:", "not in the corpu") , ("64 bytes", hit64, string.repeat "s" 64) ] fn case { let (label, hit, miss) = case per_call "stringzilla.find, {label}, hit" (fn _ { at corpus hit }) per_call "string.contains, {label}, hit" ( fn _ { if string.contains corpus hit { 1 } else { 0 } } ) per_call "stringzilla.find, {label}, miss" (fn _ { at corpus miss }) per_call "string.contains, {label}, miss" ( fn _ { if string.contains corpus miss { 1 } else { 0 } } ) } // The dedicated byte search for the 1-byte needle. per_call "stringzilla.find_byte, 1 byte, hit" (fn _ { option.unwrap_or (stringzilla.find_byte corpus 120) 0 }) // 10,000 strings, reverse-sorted with ties ("k10000" .. "k1" paddings // share prefixes). let words = list.map (list.reverse (list.range 1 10000)) fn i { "k{i}" } per_call_sort "stringzilla.sort, 10k strings" words per_call_sort "list.sort, 10k strings" words } fn per_call_sort :: label: String words: List -> () / { let start = time.now () let sorted = if label == "list.sort, 10k strings" { Ok(list.sort words) } else { stringzilla.sort words } let ns = time.in_nanoseconds (time.since (time.now ()) start) match sorted { Ok(xs) -> io.println "{label}: {ns} ns per call ({list.length xs})" , Error(e) -> panic e } }