با سلام.
سوال : در کد زیر چرا خروجی دومی انقدر سریعتر از اولی هست ؟ در صورتی که هردو تقریبا یک کار رو انجام میدن و یه نتیجه یکسان دارن:

from timeit import timeit

lst = [0] * 10000000

txt1 = "any(i % 2 for i in lst)"
txt2 = "any(True for i in lst if i % 2)"

print(timeit(txt1, globals=globals(), number=2))
print(timeit(txt2, globals=globals(), number=2))

Comments

Be the first to add a comment