OILS / test / bug-2123.ysh View on Github | oilshell.org

78 lines, 52 significant
1# Run with _bin/cxx-opt/ysh
2
3proc filter (; predicate) {
4 for line in (io.stdin) {
5 if (io->evalExpr(predicate, vars={_val: fromJson8(line)})) {
6 write -- $line
7 }
8 }
9}
10
11#
12# Setup
13#
14
15var f = '_tmp/b1'
16
17for i in (0 ..= 100_000) {
18 json write ({}, space=0)
19} > $f
20
21#
22# Benchmarks
23#
24
25var N = 2
26
27proc with-default {
28 write -- u'get() with default\n'
29 write -- 'BEFORE ------------------------------------------------------'
30
31 for _ in (0 ..< N) {
32 time for line in (io.stdin) {
33 call fromJson8(line)
34 } < $f
35 write
36 }
37
38 echo 'FILTER with default'
39 time filter [get(_val, 'missing-key', 0) === 0] < $f >/dev/null
40
41 write -- 'AFTER -------------------------------------------------------'
42
43 for _ in (0 ..< N) {
44 time for line in (io.stdin) {
45 call fromJson8(line)
46 } < $f
47 write
48 }
49}
50
51proc without-default {
52 write -- u'get() without default\n'
53 write -- 'BEFORE ------------------------------------------------------'
54
55 for _ in (0 ..< N) {
56 time for line in (io.stdin) {
57 call fromJson8(line)
58 } < $f
59 write
60 }
61
62 echo 'FILTER without default'
63 time filter [get(_val, 'missing-key')] < $f #>/dev/null
64
65 write -- 'AFTER -------------------------------------------------------'
66
67 for _ in (0 ..< N) {
68 time for line in (io.stdin) {
69 call fromJson8(line)
70 } < $f
71 write
72 }
73}
74
75
76with-default
77without-default
78