forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient_benchmark.nu
158 lines (147 loc) · 5.62 KB
/
gradient_benchmark.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Kubouch wrote this on/around 01/26/2022
def iter_inc [incr mult iter] {
$incr + $mult * $iter
}
let is_release = (input "Did you compile in a release mode? y/n ")
if ($is_release | str downcase | str trim) == "y" {
print $"running test 0 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 0. this has wrong output
let 0 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = (iter_inc 2 2 $col)
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m(char sp)(ansi -e '0m')"
}
} | str join)
$"($row_data)(char newline)"
} | str join
}} | math avg)
print $"running test 1 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 1. Fixed newline to fix the output (char cr)
let 1 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = (iter_inc 2 2 $col)
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m(char sp)(ansi -e '0m')"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print $"running test 2 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 2. Replace (char sp) with just space
let 2 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = (iter_inc 2 2 $col)
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m (ansi -e '0m')"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print $"running test 3 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 3. Precompute (ansi -e '48;2;0;0;') and (ansi -e '0m') -- seems to be slower
let 3 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
let ansi1 = (ansi -e '48;2;0;0;')
let ansi2 = (ansi -e '0m')
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = (iter_inc 2 2 $col)
if $fgcolor > 200 and $fgcolor < 210 {
$"($ansi1)($fgcolor)m($stamp)($ansi2)"
} else {
$"($ansi1)($fgcolor)m(char sp)($ansi2)"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print $"running test 4 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 4. Inline iter_inc call
let 4 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = 2 + 2 * $col
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m(char sp)(ansi -e '0m')"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print $"running test 5 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 5. Combine (char sp) substitution and iter_inc inlining
let 5 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | each { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = 2 + 2 * $col
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m (ansi -e '0m')"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print $"running test 6 at (date now | format date '%Y-%m-%d %H:%M:%S.%3f')"
# 6. The above with par-each outer loop (using par-each anywhere else breaks the output)
let 6 = (seq 1 10 | each { timeit {
let height = 40
let width = 160
let stamp = 'Nu'
seq 0 $height | par-each -t 100 { |row|
let row_data = (seq 0 $width | each { |col|
let fgcolor = 2 + 2 * $col
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m (ansi -e '0m')"
}
} | str join)
$"($row_data)(char cr)"
} | str join
}} | math avg)
print 'collating tests'
{
wrong_output: $0
newline: $1
span: $2
precompute_ansi: $3
inline_call: $4
space_and_inline_call: $5
par_each: $6
}
} else {
print "Compile in a release mode!"
}