File: stack.bench

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (241 lines) | stat: -rw-r--r-- 5,991 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- tcl -*-
# Tcl Benchmark File
#
# This file contains a number of benchmarks for the 'struct::stack'
# data structure to allow developers to monitor package performance.
#
# (c) 2008-2010 Andreas Kupries <andreas_kupries@users.sourceforge.net>


# We need at least version 8.2 for the package and thus the
# benchmarks.


# ### ### ### ######### ######### ######### ###########################
## Setting up the environment ...

package require Tcl 8.5 9

package forget struct::list
package forget struct::stack

set self  [file join [pwd] [file dirname [info script]]]
set mod   [file dirname $self]
set index [file join [file dirname $self] tcllibc pkgIndex.tcl]

if 1 {
    if {[file exists $index]} {
	set ::dir [file dirname $index]
	uplevel #0 [list source $index]
	unset ::dir
	package require tcllibc
    }
}

source [file join $mod  cmdline cmdline.tcl]
source [file join $self list.tcl]
source [file join $self stack.tcl]

# ### ### ### ######### ######### ######### ###########################
## Create a few helpers

proc makeNcmd {n} {
    return [linsert [struct::list iota $n] 0 s push]
}

proc makeN {n} {
    struct::stack s
    if {$n > 0} { eval [makeNcmd $n] }
    return
}

# ### ### ### ######### ######### ######### ###########################
## Get all the possible implementations

struct::stack::SwitchTo {}
foreach e [struct::stack::KnownImplementations] {
    ::struct::stack::LoadAccelerator $e
}

# ### ### ### ######### ######### ######### ###########################
## Benchmarks.

# We have only 6 stack operations
#
# * peek   - Retrieve N elements, keep on stack, N > 0
# * pop    - Destructively retrieve N elements, N > 0
# * push   - Add N elements to the stack, N > 0
# * rotate - Rotate the top N elements K steps, N > 1, K > 0
# * size   - Query the size of the stack.
# * clear  - Remove all elements from the stack.
# * get    - Alternate API to peek, retrieve whole stack
# * trim   - Alternate API to pop, set to specific size, return deleted elements.

# peek/pop:
# - Time to retrieve/remove 1/10/100/1000 elements incrementally from a stack.
# - Time to retrieve/remove ............. elements at once from a stack.
# - Stack sizes 10/100/1000/1000 and pop only elements less than size.
# Expected: Amortized linear time in number of retrieved/removed elements.

foreach stackimpl [struct::stack::Implementations] {
    struct::stack::SwitchTo $stackimpl

    foreach base {10 100 1000 10000} {

	bench -desc "stack trim once $base/0 stack($stackimpl)" -ipre {
	    makeN $base
	} -body {
	    s trim 0
	} -ipost {
	    s destroy
	}

	foreach remove {1 10 100 1000 10000} {
	    if {$remove > $base} continue

	    bench -desc "stack pop once $base/$remove stack($stackimpl)" -ipre {
		makeN $base
	    } -body {
		s pop $remove
	    } -ipost {
		s destroy
	    }

	    set newsize [expr {$base - $remove}]

	    bench -desc "stack trim once $base/$remove stack($stackimpl)" -ipre {
		makeN $base
	    } -body {
		s trim $newsize
	    } -ipost {
		s destroy
	    }

	    bench -desc "stack pop incr $base/$remove stack($stackimpl)" -pre {
		set cmd {}
		foreach x [struct::list iota $remove] {
		    lappend cmd [list s pop]
		}
		proc foo {} [join $cmd \n]
		catch {foo} ;# compile
	    } -ipre {
		makeN $base
	    } -body {
		foo
	    } -ipost {
		s destroy
	    } -post {
		rename foo {}
	    }

	    bench -desc "stack peek $base/$remove stack($stackimpl)" -ipre {
		makeN $base
	    } -body {
		s peek $remove
	    } -ipost {
		s destroy
	    }
	}

	bench -desc "stack get $base stack($stackimpl)" -ipre {
	    makeN $base
	} -body {
	    s get
	} -ipost {
	    s destroy
	}
    }

    # rotate
    # - Time to rotate 1,N/4,N/2,N-1 steps of 10/100/1000 elements atop 10/100/1000/10000
    # Expected: Linear time in number of rotating elements.
    # C:   As expected.
    # Tcl: Linear in both number of rotating elements and number of steps! Fix this.

    foreach n {10 100 1000 10000} {
	foreach top {10 100 1000} {
	    if {$top > $n} continue
	    foreach s [list 1 [expr {$top >> 2}] [expr {$top >> 1}] [expr {$top - 1}]] {
		bench -desc "stack rotate $n/$top/$s stack($stackimpl)" -pre {
		    makeN $n
		} -body {
		    s rotate $top $s
		} -post {
		    s destroy
		}
	    }
	}
    }

    # push:
    # - Time to add 1/10/100/1000 elements incrementally to an empty stack
    # - Time to add ............. elements at once to an empty stack.
    # - As above, to a stack containing 1/10/100/1000 elements already.
    # Expected: Amortized linear time in number of elements added.

    foreach base  {0 1 10 100 1000 10000} {
	foreach add {1 10 100 1000 10000} {
	    bench -desc "stack push once $base/$add stack($stackimpl)" -ipre {
		makeN $base
		set cmd [makeNcmd $add]
	    } -body {
		eval $cmd
	    } -ipost {
		s destroy
	    }
	    bench -desc "stack push incr $base/$add stack($stackimpl)" -pre {
		set cmd {}
		foreach x [struct::list iota $add] {
		    lappend cmd [list s push $x]
		}
		proc foo {} [join $cmd \n]
		catch {foo} ;# compile
	    } -ipre {
		makeN $base
	    } -body {
		foo
	    } -ipost {
		s destroy
	    } -post {
		rename foo {}
	    }
	}
    }

    # size
    # - Time to query size of stack containing 0/1/10/100/1000/10000 elements.
    # Expected: Constant time.

    foreach n {0 1 10 100 1000 10000} {
	bench -desc "stack size $n stack($stackimpl)" -pre {
	    makeN $n
	} -body {
	    s size
	} -post {
	    s destroy
	}
    }

    # clear
    # - Time to clear a stack containing 0/1/10/100/1000/10000 elements.
    # Expected: Constant to linear time in number of elements to clear.

    foreach n {0 1 10 100 1000 10000} {
	bench -desc "stack clear $n stack($stackimpl)" -ipre {
	    makeN $n
	} -body {
	    s clear
	} -ipost {
	    s destroy
	}
    }

} ;# End of loop over stack implementations

# ### ### ### ######### ######### ######### ###########################
## Complete

return

# ### ### ### ######### ######### ######### ###########################
## Notes ...