Clone this repository, and run these scripts:
$ python3 test1.py
$ python3 test2.pyAnd see the differences?
| __pycache__/ |
| from test_functions import * | |
| results = test_for_n_times(10, func_1) | |
| print_results(results) |
| from test_functions import * | |
| results = test_for_n_times(10, func_2) | |
| print_results(results) |
| import time | |
| s = '' | |
| N = 1000000 | |
| def func_1(): | |
| [ print(s, end='') for i in range(N) ] | |
| def func_2(): | |
| for i in range(N): | |
| print(s, end='') | |
| def test_for_n_times(n, foo): | |
| for i in range(n): | |
| st = time.time() | |
| foo() | |
| yield (time.time() - st) | |
| def print_results(l): | |
| l = list(l) | |
| for val in l: | |
| print('\t{:.3f}'.format(val)) | |
| print('Avg:\t{:.3f}'.format(sum(l)/len(l))) |