1 | # Run with _bin/cxx-opt/ysh
|
2 |
|
3 | proc 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 |
|
15 | var f = '_tmp/b1'
|
16 |
|
17 | for i in (0 ..= 100_000) {
|
18 | json write ({}, space=0)
|
19 | } > $f
|
20 |
|
21 | #
|
22 | # Benchmarks
|
23 | #
|
24 |
|
25 | var N = 2
|
26 |
|
27 | proc 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 |
|
51 | proc 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 |
|
64 | for line in (io.stdin) {
|
65 | var d = fromJson8(line)
|
66 | call get(d, 'missing-key')
|
67 | } < $f
|
68 |
|
69 | #time filter [get(_val, 'missing-key', 0) === 0] < $f >/dev/null
|
70 | time filter [get(_val, 'missing-key')] < $f #>/dev/null
|
71 |
|
72 | write -- 'AFTER -------------------------------------------------------'
|
73 |
|
74 | for _ in (0 ..< N) {
|
75 | time for line in (io.stdin) {
|
76 | call fromJson8(line)
|
77 | } < $f
|
78 | write
|
79 | }
|
80 | }
|
81 |
|
82 |
|
83 | with-default
|
84 | without-default
|
85 |
|