File: sets.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 (428 lines) | stat: -rw-r--r-- 11,540 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# -*- tcl -*-
# Tcl Benchmark File
#
# This file contains a number of benchmarks for the 'struct::set'
# data structure to allow developers to monitor package performance.
#
# (c) 2007-2010 Andreas Kupries <andreas_kupries@users.sourceforge.net>

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

if {![package vsatisfies [package provide Tcl] 8.5 9]} {
    return
}

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

set moddir [file dirname [file dirname [info script]]]
lappend auto_path $moddir

package forget struct::set

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 $self sets.tcl]

# ### ### ### ######### ######### ######### ###########################
# Helper commands to build various types of sets.

proc makeN {n {times 1}} {
    set res {}
    for {set i 0} {$i < $times} {incr i} {
	for {set j 1} {$j <= $n} {incr j} {
	    lappend res $j
	}
    }
    return $res
}

# Select between configurations for quick overview vs full test

#set xtime {1 2}
#set xlen  {1 10 100}
set xtime {1 2 3}
set xlen  {1 10 100 1000}
#set xtime {1 2 3 4}
#set xlen  {1 10 100 1000 10000}

foreach times $xtime {
    foreach n $xlen {
	set sx($times,$n) [makeN $n $times]
    }
}

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

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

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

# empty
# size = cardinality
# contains
# union
# intersect
# difference
# symdiff
# intersect3
# equal
# include, add
# exclude, subtract
# subsetof

foreach setimpl [struct::set::Implementations] {
    struct::set::SwitchTo $setimpl

    # ### ### ### ######### ######### ######### ###########################
    ## empty

    bench -desc "set empty set($setimpl)" -body {
	struct::set empty {}
    }

    if {$setimpl eq "tcl"} {
	# Not useable for a critcl implementation.
	bench -desc "set empty, raw set($setimpl)" -body {
	    struct::set::S_empty {}
	}
    }
    # ### ### ### ######### ######### ######### ###########################
    ## cardinality

    foreach times $xtime {
	foreach n $xlen {
	    bench -desc "set size x$times $n set($setimpl)" -body {
		struct::set size $sx($times,$n)
	    }
	}
    }

    # ### ### ### ######### ######### ######### ###########################
    ## contains

    foreach times $xtime {
	foreach n $xlen {
	    bench -desc "set contains, not,   x$times $n set($setimpl)" -body {
		struct::set contains  $sx($times,$n) 0
	    }
	    bench -desc "set contains, early, x$times $n set($setimpl)" -body {
		struct::set contains  $sx($times,$n) 1
	    }
	    bench -desc "set contains, last,  x$times $n set($setimpl)" -body {
		struct::set contains  $sx($times,$n) $n
	    }
	}
    }

    # ### ### ### ######### ######### ######### ###########################
    ## union
    # cases: no intersection, partial intersection, equal sets, subsets
    # and always a varying number of duplicates.


    # ### ### ### ######### ######### ######### ###########################
    ## intersect

    # ### ### ### ######### ######### ######### ###########################
    ## difference

    # ### ### ### ######### ######### ######### ###########################
    ## symdiff

    # ### ### ### ######### ######### ######### ###########################
    ## intersect3

    # ### ### ### ######### ######### ######### ###########################
    ## equal

    foreach times $xtime {
	foreach n $xlen {
	    bench -desc "set equal, yes,  x$times $n set($setimpl)" -body {
		struct::set equal $sx($times,$n) $sx($times,$n) 
	    }
	    # sets have no intersection
	    bench -desc "set equal, no1,   x$times $n set($setimpl)" -body {
		struct::set equal $sx($times,$n) {a b c d e}
	    }
	    # second set is either true subset, or true superset
	    bench -desc "set equal, no2,   x$times $n set($setimpl)" -body {
		struct::set equal $sx($times,$n) {1 2 3 4}
	    }
	}
    }

    # ### ### ### ######### ######### ######### ###########################
    ## include, add

    foreach times $xtime {
	foreach n $xlen {

	    # Adding/including known items should be fast, as nothing
	    # changes. It should even be fast in case of a shared
	    # object. Which we have in A btw.

	    bench -desc "set include, known x$times $n set($setimpl)" -pre {
		set A $sx($times,$n)
		struct::set include A x
	    } -body {
		struct::set include A x
	    } -post {
		unset A
	    }
	    bench -desc "set add, known x$times $n set($setimpl)" -pre {
		set A $sx($times,$n)
		struct::set add A {a b c d e}
	    } -body {
		struct::set add A {a b c d e}
	    } -post {
		unset A
	    }

	    # Now adding/including items not yet in the set is affected
	    # much more by the environment. I.e: Is the object shared ?
	    # And: Is the object already in set-type ? Four possibilities.

	    # (a) S/U - shared/unshared
	    # (b) S/C - set/string (c for conversion required)

	    # Notes on the results:
	    #
	    # I.   <SC> - duplication&conversion - time goes up with set size
	    # II.  <SS> - duplication            - s.a
	    # III. <UC> - conversion             - s.a, but with larger constant
	    # IV.  <US> - near constant - likely linear in the size of the set added.
	    #
	    # The times for I-III ramp up rapidly enough to make Critcl
	    # slower than Tcl for a constant set containing somewhere between
	    # 100-1000 elements. This however is only of consequence to
	    # one-shot set operations. In case of multiple operations only
	    # the first one incurs the above costs, any operation coming
	    # after is fast, see IV. I.e.Tcl keeps on adding large times
	    # to the total, Critcl otoh goes flat. IOW Critcl may incur a
	    # high startup cost when starting with large constant sets,
	    # but amortizes this then over all future operations.

	    # Note 2: Most of the other benchmarks do not measure
	    # conversion time, because the first untimed execution of a
	    # body forces not only bc compilation of the script, but also
	    # the input to set-type already (values held in the array
	    # 'sx').

	    # --

	    # I. shared string-type <SC>

	    bench -desc "set include, missing <SC> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
		set B $A
	    } -body {
		struct::set include A x
	    } -ipost {
		unset A B
	    }
	    bench -desc "set add, missing <SC> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
		set B $A
	    } -body {
		struct::set add A {a b c d e}
	    } -ipost {
		unset A B
	    }

	    # II. shared set-type <SS>

	    bench -desc "set include, missing <SS> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
	    } -body {
		struct::set include A x
	    } -ipost {
		unset A
	    }
	    bench -desc "set add, missing <SS> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
	    } -body {
		struct::set add A {a b c d e}
	    } -ipost {
		unset A
	    }

	    # III. unshared string-type <UC>

	    bench -desc "set include, missing <UC> x$times $n set($setimpl)" -ipre {
		# string range creates new unshared duplicate in A.
		set A [string range $sx($times,$n) 1 end]
	    } -body {
		struct::set include A x
	    } -ipost {
		unset A
	    }
	    bench -desc "set add, missing <UC> x$times $n set($setimpl)" -ipre {
		set A [string range $sx($times,$n) 1 end]
	    } -body {
		struct::set add A {a b c d e}
	    } -ipost {
		unset A
	    }

	    # IV. unshared set-type <US>

	    bench -desc "set include, missing <US> x$times $n set($setimpl)" -ipre {
		# string range creates new unshared duplicate in A.
		# Adding the empty set forces the value of A to set-type.
		set A [string range $sx($times,$n) 1 end]
		struct::set add A {}
	    } -body {
		struct::set include A x
	    } -ipost {
		unset A
	    }
	    bench -desc "set add, missing <US> x$times $n set($setimpl)" -ipre {
		set A [string range $sx($times,$n) 1 end]
		struct::set add A {}
	    } -body {
		struct::set add A {a b c d e}
	    } -ipost {
		unset A
	    }
	}
    }

    # ### ### ### ######### ######### ######### ###########################
    ## exclude, subtract

    foreach times $xtime {
	foreach n $xlen {

	    # Subtracting/excluding unknown items should be fast, as
	    # nothing changes. It should even be fast in case of a shared
	    # object. Which we have in A btw.

	    bench -desc "set exclude, missing x$times $n set($setimpl)" -pre {
		set A $sx($times,$n)
	    } -body {
		struct::set exclude A x
	    } -post {
		unset A
	    }
	    bench -desc "set subtract, missing x$times $n set($setimpl)" -pre {
		set A $sx($times,$n)
	    } -body {
		struct::set subtract A {a b c d e}
	    } -post {
		unset A
	    }

	    # Now subtracting/excluding items in the set is affected
	    # much more by the environment. I.e: Is the object shared ?
	    # And: Is the object already in set-type ? Four possibilities.

	    # See above for discussion.
	    # --

	    # I. shared string-type <SC>

	    bench -desc "set exclude, known <SC> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
		set B $A
	    } -body {
		struct::set exclude A 1
	    } -ipost {
		unset A B
	    }
	    bench -desc "set subtract, known <SC> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
		set B $A
	    } -body {
		struct::set subtract A {1 2 3 4 5}
	    } -ipost {
		unset A B
	    }

	    # II. shared set-type <SS>

	    bench -desc "set exclude, known <SS> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
	    } -body {
		struct::set exclude A 1
	    } -ipost {
		unset A
	    }
	    bench -desc "set subtract, known <SS> x$times $n set($setimpl)" -ipre {
		set A $sx($times,$n)
	    } -body {
		struct::set subtract A {1 2 3 4 5}
	    } -ipost {
		unset A
	    }

	    # III. unshared string-type <UC>

	    bench -desc "set exclude, known <UC> x$times $n set($setimpl)" -ipre {
		# string range creates new unshared duplicate in A.
		set A [string range $sx($times,$n) 1 end]
	    } -body {
		struct::set exclude A 1
	    } -ipost {
		unset A
	    }
	    bench -desc "set subtract, known <UC> x$times $n set($setimpl)" -ipre {
		set A [string range $sx($times,$n) 1 end]
	    } -body {
		struct::set subtract A {1 2 3 4 5}
	    } -ipost {
		unset A
	    }

	    # IV. unshared set-type <US>

	    bench -desc "set exclude, known <US> x$times $n set($setimpl)" -ipre {
		# string range creates new unshared duplicate in A.
		# Adding the empty set forces the value of A to set-type.
		set A [string range $sx($times,$n) 1 end]
		struct::set add A {}
	    } -body {
		struct::set exclude A 1
	    } -ipost {
		unset A
	    }
	    bench -desc "set subtract, known <US> x$times $n set($setimpl)" -ipre {
		set A [string range $sx($times,$n) 1 end]
		struct::set add A {}
	    } -body {
		struct::set subtract A {1 2 3 4 5}
	    } -ipost {
		unset A
	    }
	}
    }

    # ### ### ### ######### ######### ######### ###########################
    ## subsetof

}

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

return

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