tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

120 lignes · 3601 octetsBrut · Historique
1// Find in a 4.5 MB corpus, StringZilla against `std/string`'s contains
2// (a memmem-style two-way search in the runtime): needle lengths 1, 4,
3// 16 and 64, found near the end or absent; then a 10k-string sort
4// against `list.sort`. Not a test: build it with `louise build
5// --release` in this directory and run `build/stringzilla_bench`. Each
6// line is the time per call; the number in brackets adds up what the
7// calls gave back, so the work cannot be skipped.
8//
9// `string.contains` answers a Bool where `stringzilla.find` answers the
10// offset: same scan, a trivially cheaper result. WS-A's SIMD
11// `string.find` prim (issue gabla108/louise#380) had not landed when this was written;
12// add its line here when it has.
13
14import std/io
15import std/list
16import std/option
17import std/string
18import std/time.{type clock}
19import stringzilla
20
21// Calls per measurement.
22fn rounds :: -> Int
23{
24 50
25}
26
27fn per_call ::
28 label: String
29 work: fn(Int) -> Int / <ffi>
30 -> () / <io, clock, ffi>
31{
32 let start = time.now ()
33 let total = list.fold (list.range 1 (rounds ())) 0 fn acc i { acc + work i }
34 let ns = time.in_nanoseconds (time.since (time.now ()) start) / rounds ()
35 io.println "{label}: {ns} ns per call ({total})"
36}
37
38/// The offset of `needle`, or 0: the work `find` does, as an Int.
39fn at :: corpus: String needle: String -> Int / <ffi>
40{
41 option.unwrap_or (stringzilla.find corpus needle) 0
42}
43
44pub fn main :: -> () / <io, clock, ffi>
45{
46 io.println
47 "stringzilla {stringzilla.version ()} ({stringzilla.capabilities ()})"
48 // 45 bytes × 100,000 = 4.5 MB; the hit needles occur once, at the end.
49 let chunk = "the quick brown fox jumps over the lazy dog. "
50 let corpus = string.repeat chunk 100000 <> "one buried landmark: xylophone"
51 let hit64 =
52 string.slice corpus (string.length corpus - 64) (string.length corpus)
53 list.each
54 [ ("1 byte", "x", "1")
55 , ("4 bytes", "land", "zzzz")
56 , ("16 bytes", "buried landmark:", "not in the corpu")
57 , ("64 bytes", hit64, string.repeat "s" 64)
58 ]
59 fn case
60 {
61 let (label, hit, miss) = case
62 per_call "stringzilla.find, {label}, hit" (fn _ { at corpus hit })
63 per_call
64 "string.contains, {label}, hit"
65 ( fn _
66 {
67 if string.contains corpus hit
68 {
69 1
70 }
71 else
72 {
73 0
74 }
75 }
76 )
77 per_call "stringzilla.find, {label}, miss" (fn _ { at corpus miss })
78 per_call
79 "string.contains, {label}, miss"
80 ( fn _
81 {
82 if string.contains corpus miss
83 {
84 1
85 }
86 else
87 {
88 0
89 }
90 }
91 )
92 }
93 // The dedicated byte search for the 1-byte needle.
94 per_call
95 "stringzilla.find_byte, 1 byte, hit"
96 (fn _ { option.unwrap_or (stringzilla.find_byte corpus 120) 0 })
97 // 10,000 strings, reverse-sorted with ties ("k10000" .. "k1" paddings
98 // share prefixes).
99 let words = list.map (list.reverse (list.range 1 10000)) fn i { "k{i}" }
100 per_call_sort "stringzilla.sort, 10k strings" words
101 per_call_sort "list.sort, 10k strings" words
102}
103
104fn per_call_sort :: label: String words: List<String> -> () / <io, clock, ffi>
105{
106 let start = time.now ()
107 let sorted = if label == "list.sort, 10k strings"
108 {
109 Ok(list.sort words)
110 }
111 else
112 {
113 stringzilla.sort words
114 }
115 let ns = time.in_nanoseconds (time.since (time.now ()) start)
116 match sorted
117 { Ok(xs) -> io.println "{label}: {ns} ns per call ({list.length xs})"
118 , Error(e) -> panic e
119 }
120}