View difference between Paste ID: smPKTSQN and rZDEEcDp
SHOW: | | - or go back to the newest paste.
1
(declaim (optimize (speed 3) (safety 1)))
2
3
(defun flatten-1 (l)
4
  (cond
5
    ((null l) nil)
6
    ((atom l) (list l))
7
    (t (loop for a in l appending (flatten-1 a)))))
8
9
(defun flatten-4 (l)
10
  (cond
11
    ((null l) nil)
12
    ((atom l) (list l))
13
    (t (loop for a in l nconcing (flatten-4 a)))))
14
15
(defun flatten-2 (l)
16
  (when l
17
    (if (atom l)
18
	(list l)
19
	(mapcan #'flatten-2 l))))
20
21
(defun flatten (x &optional y)
22
  (cond
23
    ((null x)
24
     (cond
25-
(let ((test (get-internal-real-time)))
25+
26-
  (dotimes (i 1e6)
26+
27-
    (flatten '(1 2 (3 4) (5 (6 (7 8) 9 a (10))) b (10.5 (1.6) (1.7)) 11)))
27+
28-
  (print (- (get-internal-real-time) test))
28+
29-
  (setf test (get-internal-real-time))
29+
30-
  (dotimes (i 1e6)
30+
31-
    (flatten-2 '(1 2 (3 4) (5 (6 (7 8) 9 a (10))) b (10.5 (1.6) (1.7)) 11)))
31+
32-
  (print (- (get-internal-real-time) test))
32+
33-
  (setf test (get-internal-real-time))
33+
(defun flatten-5 (x &aux (res (list nil)))
34-
  (dotimes (i 1e6)
34+
  (do ((x x) (y nil) (r res))
35-
    (flatten-1 '(1 2 (3 4) (5 (6 (7 8) 9 a (10))) b (10.5 (1.6) (1.7)) 11)))
35+
      (nil)
36-
  (print (- (get-internal-real-time) test)))
36+
   (cond 
37
    ((null x)
38-
;; 886 
38+
39-
;; 2868 
39+
       ((null y) (return-from flatten-5 (cdr res)))
40-
;; 1828
40+
41
        (setq x (car y) y (cdr y)))
42
       (t (rplacd r (list (car y)))
43
          (setq r (cdr r)
44
                x (cdr y)
45
                y nil))))
46
    ((listp (car x))
47
     (setq y (if y (list (cdr x) y) (cdr x))
48
           x (car x)))
49
    (t (rplacd r (list (car x)))
50
       (setq r (cdr r)
51
             x (cdr x))))))
52
53
54
(defvar tt '(1 2 (3 4) (5 (6 (7 8) 9 a (10))) b (10.5 (1.6) (1.7)) 11))
55
(defvar ttt (append tt tt tt tt tt tt tt tt tt tt tt tt tt))
56
(defvar tttt (append ttt ttt ttt ttt ttt ttt ttt ttt ttt ttt ttt ttt ttt))
57
58
(defun run-test(n tt)
59
 (let ((test (get-internal-run-time)) a b c d e ta tb tc td te)
60
  (dotimes (i n)
61
    (setf a (flatten tt)))
62
  (print (setf ta (- (get-internal-run-time) test)))
63
  (princ "       linear rec")
64
65
  (setf test (get-internal-run-time))
66
  (dotimes (i n)
67
    (setf b (flatten-5 tt)))
68
  (print (setf tb (- (get-internal-run-time) test)))
69
  (princ "       top-down loop")
70
71
  (setf test (get-internal-run-time))
72
  (dotimes (i n)
73
    (setf c (flatten-2 tt)))
74
  (print (setf tc (- (get-internal-run-time) test)))
75
  (princ "       mapcan")
76
77
  (setf test (get-internal-run-time))
78
  (dotimes (i n)
79
    (setf d (flatten-4 tt)))
80
  (print (setf td (- (get-internal-run-time) test)))
81
  (princ "       nconcing")
82
83
  (setf test (get-internal-run-time))
84
  (dotimes (i n)
85
    (setf e (flatten-1 tt)))
86
  (print (setf te (- (get-internal-run-time) test)))
87
  (princ "       appending")
88
89
  (print (mapcar #'(lambda(x)(equal a x)) (list b c d e)))
90
  (print (mapcar #'(lambda(x)(/ x (+ tc 0.0))) (list ta tb tc td te)))
91
  (values)))
92
93
#|
94
95
[23]> (run-test 1e5 tt)
96
97
8612384        linear rec
98
9313392        top-down loop
99
6709648        mapcan
100
10515120        nconcing
101
16724048        appending
102
(T T T T)
103
(1.2835821 1.3880597 1.0 1.5671642 2.4925373)
104
105
106
[8]> (run-test 1e4 ttt)
107
108
11015840        linear rec
109
11416416        top-down loop
110
8211808        mapcan
111
13219008        nconcing
112
21530960        appending
113
(T T T T)
114
(1.3414634 1.3902439 1.0 1.6097561 2.621951)
115
116
117
[3]> (run-test 1e3 tttt)
118
119
17625344        linear rec
120
15021600        top-down loop
121
11115984        mapcan
122
17725488        nconcing
123
30143344        appending
124
(T T T T)
125
(1.5636364 1.3636364 1.0 1.6090909 2.790909)
126
127
---------------------------------------------
128
for growing sizes of data,
129
"top-down loop", "mapcan" and "nconcing" run with SAME complexity,
130
"linear rec" and "appending"  --  with WORSE.
131
----------------------------------------------
132
|#
133
134
;; results
135
;; 886        linear rec
136
;; 2868       mapcan
137
;; 1828       appending