Debian Codegraph @

test/comparator-performance.scm

raw
1 ;;
2 ;; For optimizing comparator comparison procedures
3 ;;
4
5 (use gauche.time)
6 (use srfi.42)
7
8 (define-constant *count* 1000000)
9
10 (define (binary cmp a b)
11 (^[] (dotimes [*count*]
12 (list
13 (=? cmp a b)
14 (>? cmp a b)
15 (>=? cmp a b)
16 (<? cmp a b)
17 (<=? cmp a b)))))
18
19 (define (binary-primitive a b)
20 (^[] (dotimes [*count*]
21 (list
22 (= a b)
23 (> a b)
24 (>= a b)
25 (< a b)
26 (<= a b)))))
27
28 (define (ternary cmp a b c)
29 (^[] (dotimes [*count*]
30 (list
31 (=? cmp a b c)
32 (>? cmp a b c)
33 (>=? cmp a b c)
34 (<? cmp a b c)
35 (<=? cmp a b c)))))
36
37 (define (ternary-primitive a b c)
38 (^[] (dotimes [*count*]
39 (list
40 (= a b c)
41 (> a b c)
42 (>= a b c)
43 (< a b c)
44 (<= a b c)))))
45
46 (define (main args)
47 ($ time-these/report 1
48 `((primitive/2 . ,(binary-primitive 0 1))
49 (default/2 . ,(binary default-comparator 0 1))
50 (integer/2 . ,(binary integer-comparator 0 1))
51 (number/2 . ,(binary number-comparator 0 1))
52 (custom1/2 . ,(binary (make-comparator #t eqv? < #f) 0 1))
53 (custom2/2 . ,(binary (make-comparator integer? eqv? < #f) 0 1))
54 (primitive/3 . ,(ternary-primitive 0 1 2))
55 (default/3 . ,(ternary default-comparator 0 1 2))
56 (integer/3 . ,(ternary integer-comparator 0 1 2))
57 (number/3 . ,(ternary number-comparator 0 1 2))
58 (custom1/3 . ,(ternary (make-comparator #t eqv? < #f) 0 1 2))
59 (custom2/3 . ,(ternary (make-comparator integer? eqv? < #f) 0 1 2))))
60 )