Review the basic operations of Python only for the parts that you are uneasy about. As the first step, we describe list comprehension notation.

・ Speeding up processing ・ Simplification of structure Mainly because these benefits may be obtained. Here, the description method and evaluation will be performed.

[Variable processing]
Example)
`data = [i for i in range (10)]`

[Variable processing conditions]
Example)
`data = [i for i in range (10) if i%2 == 0]`

[Variable condition processing]
Example)
`data = [i if i%2 == 0 else 0 for i in range (10)]`

`List_Comprehension.py`

```
"""
2020/12/13
@Yuya Shimizu
List comprehension
"""
#Description method 1:[Variable processing]
data = [i for i in range(10)]
print(data)
#Description method 2:[Variable processing conditions]
data = [x for x in range(10) if x%2 == 0]
print(data)
#Description method 3:[Variable condition processing]
data = [y if y%2 == 0 else "xxx" for y in range(10)]
print(data)
```

Certainly, I think that the structure can be simplified with one line. But is it really faster? In the following, the verification is performed using the time module. Since it was difficult to understand the difference when the number of repetitions was small, It was verified under 5 conditions of repetition number 1000, 2000, 3000, 4000, 5000.

`List_Comprehension.py`

```
"""
2020/12/13
@Yuya Shimizu
List comprehension
"""
iteration = 1000 #Repeat condition{1000,2000,3000,4000,5000}
def func1():
#[Variable processing]
data = []
for i in range(iteration):
data.append(i)
print(data)
#[Variable processing conditions]
data = []
for x in range(iteration):
if x%2 == 0:
data.append(x)
print(data)
#[Variable condition processing]
data = []
for y in range(iteration):
if y%2 == 0:
data.append(y)
else:
data.append("xxx")
print(data)
def func2():
#[Variable processing]
data = [i for i in range(iteration)]
print(data)
#[Variable processing conditions]
data = [x for x in range(iteration) if x%2 == 0]
print(data)
#[Variable condition processing]
data = [y if y%2 == 0 else "xxx" for y in range(iteration)]
print(data)
######Execution time measurement
import time
start1 = time.time()
func1()
process_time1 = time.time() - start1
start2 = time.time()
func2()
process_time2 = time.time() - start2
print("List comprehension(iteration={})----None:{}Seconds, Yes:{}Seconds".format(iteration, process_time1, process_time2))
```

Number of repetitions[Times] | Execution time without list comprehension[s] | Execution time with list comprehension[s] |
---|---|---|

1000 | 1.139344 | 1.121223 |

2000 | 1.026536 | 0.578534 |

3000 | 0.856933 | 0.884612 |

4000 | 0.766533 | 0.579598 |

5000 | 0.667517 | 0.605027 |

Speeding up is certainly seen at 2000 and 4000 times, but it is not always possible. It can be seen that the speed may be increased. In fact, if you look closely, the result is that when the number of trials is 3000, it is rather slower to use the list comprehension notation.

According to the verification results, it is not always possible to increase the speed, but if there is a possibility and the delay is slight, I wondered if it should be utilized. Besides, I think that the simplification of the structure is convenient for debugging. If there are situations where it can be used, I would definitely like to use list comprehension notation.

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts