Debian Codegraph @

tests/assemble1.bench

raw
1 proc ulam1 {n} {
2 set max $n
3 while {$n != 1} {
4 if {$n > $max} {
5 set max $n
6 }
7 if {$n % 2} {
8 set n [expr {3 * $n + 1}]
9 } else {
10 set n [expr {$n / 2}]
11 }
12 }
13 return $max
14 }
15
16 set tcl_traceCompile 2; ulam1 1; set tcl_traceCompile 0
17
18 proc ulam2 {n} {
19 tcl::unsupported::assemble {
20 load n; # max
21 dup; # max n
22 jump start; # max n
23
24 label loop; # max n
25 over 1; # max n max
26 over 1; # max in max n
27 ge; # man n max>=n
28 jumpTrue skip; # max n
29
30 reverse 2; # n max
31 pop; # n
32 dup; # n n
33
34 label skip; # max n
35 dup; # max n n
36 push 2; # max n n 2
37 mod; # max n n%2
38 jumpTrue odd; # max n
39
40 push 2; # max n 2
41 div; # max n/2 -> max n
42 jump start; # max n
43
44 label odd; # max n
45 push 3; # max n 3
46 mult; # max 3*n
47 push 1; # max 3*n 1
48 add; # max 3*n+1
49
50 label start; # max n
51 dup; # max n n
52 push 1; # max n n 1
53 neq; # max n n>1
54 jumpTrue loop; # max n
55
56 pop; # max
57 }
58 }
59 set tcl_traceCompile 2; ulam2 1; set tcl_traceCompile 0
60
61 proc test1 {n} {
62 for {set i 1} {$i <= $n} {incr i} {
63 ulam1 $i
64 }
65 }
66 proc test2 {n} {
67 for {set i 1} {$i <= $n} {incr i} {
68 ulam2 $i
69 }
70 }
71
72 for {set j 0} {$j < 10} {incr j} {
73 test1 1
74 set before [clock microseconds]
75 test1 30000
76 set after [clock microseconds]
77 puts "compiled: [expr {1e-6 * ($after - $before)}]"
78
79 test2 1
80 set before [clock microseconds]
81 test2 30000
82 set after [clock microseconds]
83 puts "assembled: [expr {1e-6 * ($after - $before)}]"
84 }