File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -7,17 +7,19 @@ In a UNIX terminal, run:
7
7
8
8
```
9
9
time python list-append-loop.py
10
- ```
10
+
11
11
real 0m11.381s
12
12
user 0m10.553s
13
13
sys 0m0.824s
14
+ ```
14
15
15
16
```
16
17
time python list-comp.py
17
- ```
18
+
18
19
real 0m4.228s
19
20
user 0m3.428s
20
21
sys 0m0.800s
22
+ ```
21
23
22
24
23
25
Here we see that the list comprehension was almost 3x as fast
@@ -31,24 +33,32 @@ numbers with the range() function, we can actually just cast the result
31
33
of the range() function to a list and get even faster speed than the
32
34
list comprehension.
33
35
36
+ ```
37
+ time python cast-list.py
38
+
39
+ real 0m2.988s
40
+ user 0m2.176s
41
+ sys 0m0.812s
42
+ ```
43
+
34
44
To show a more practical example where you can't just cast it:
35
45
36
46
```
37
47
time python list-append-loop2.py
38
- ```
39
48
40
49
real 0m29.756s
41
50
user 0m28.875s
42
51
sys 0m0.880s
52
+ ```
43
53
44
54
45
55
```
46
56
time python list-comp2.py
47
- ```
48
57
49
58
real 0m21.932s
50
59
user 0m21.031s
51
60
sys 0m0.900s
61
+ ```
52
62
53
63
In this practical example, we see that the list comprehension finished
54
64
in about 2/3 of the time it took the for loop append() method, still
You can’t perform that action at this time.
0 commit comments