Skip to content

Instantly share code, notes, and snippets.

@kLiHz
Last active May 2, 2022 17:36
Show Gist options
  • Save kLiHz/7644b2395e3c8ed420f049c807d6732a to your computer and use it in GitHub Desktop.
Save kLiHz/7644b2395e3c8ed420f049c807d6732a to your computer and use it in GitHub Desktop.
Time Consumption of Abusing Python List Deduction
__pycache__/

Time Consumption of Abusing Python List Deduction

Clone this repository, and run these scripts:

$ python3 test1.py
$ python3 test2.py

And see the differences?

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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment