-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_test.go
More file actions
119 lines (106 loc) · 1.86 KB
/
eval_test.go
File metadata and controls
119 lines (106 loc) · 1.86 KB
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
package gotcl
import (
"io"
"os"
"strings"
"testing"
)
func TestFull(t *testing.T) {
file, err := os.Open("test.tcl")
if err != nil {
t.Fatal(err)
}
_, e := NewInterp().Run(file)
if e != nil {
t.Fatal(e)
}
}
func RunString(it *Interp, s string) {
var r io.Reader = strings.NewReader(s)
_, e := it.Run(r)
if e != nil {
panic(e)
}
}
func runCmd(setup, cmd string, b *testing.B) {
b.StopTimer()
it := NewInterp()
RunString(it, setup)
v := FromStr(cmd)
it.EvalObj(v)
if it.err != nil {
panic(it.err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
it.EvalObj(v)
}
}
func Benchmark_Plus(b *testing.B) {
runCmd("", "+ 1 8", b)
}
func Benchmark_Plus4(b *testing.B) {
runCmd("", "+ 1 [+ 1 [+ 1 [+ 1 8]]]", b)
}
func Benchmark_ExprPlus4(b *testing.B) {
runCmd("", "expr { 1 + 1 + 1 + 1 + 8 }", b)
}
func Benchmark_IncrX4(b *testing.B) {
runCmd("set x 0", "incr x; incr x; incr x; incr x", b)
}
func Benchmark_Fib(b *testing.B) {
fib := `
proc fib {n} {
if { $n < 2 } {
return 1
} else {
return [+ [fib [- $n 1]] [fib [- $n 2]]]
}
}
`
runCmd(fib, "fib 17", b)
}
func Benchmark_Fib2(b *testing.B) {
fib2 := `
proc fib2 {n} {
set a 1
set b 1
for { set nn $n } { 0 < $nn } { incr nn -1 } {
set tmp [+ $a $b]
set a $b
set b $tmp
}
return $a
}
`
runCmd(fib2, "fib2 70", b)
}
func BenchmarkSumTo(b *testing.B) {
sumto := `
proc sum_to {n} {
set x 0
for { set i 0 } { $i < $n } { incr i } {
set x [+ $x $i]
}
}
`
runCmd(sumto, "sum_to 20000", b)
}
func Benchmark_SumIota(b *testing.B) {
code := `
proc iota {n} {
set result [list]
for {set i 1} { $i <= $n } { incr i } {
lappend result $i
}
return $result
}
proc sum {lst} {
set result 0
foreach x $lst {
incr result $x
}
return $result
}`
runCmd(code, "sum [iota 10000]", b)
}