gyli / PyWaffle

🧇 Make Waffle Charts in Python.
MIT License
578 stars 105 forks source link

Make mapping global variable, use builtin ceil #15

Closed tweakimp closed 4 years ago

tweakimp commented 4 years ago

I made METHOD_MAPPING global so that it is only created one time during loading time. Also math.ceil is used to replace the ceil function you wrote. :) block_iter is a generator now.

gyli commented 4 years ago

I didn't notice this but it's an interesting found with timeit

>>> timeit.timeit('int(3 // 2 + bool(3 % 2))', number=100000)
0.026131337999970583
>>> timeit.timeit('ceil(3 / 2)', number=100000, setup="from math import ceil")
0.009041516000024785
>>> timeit.timeit('3 // 2', number=100000)
0.0008856160000050295
>>> timeit.timeit('floor(3 / 2)', number=100000, setup="from math import floor")
0.008675524000011592
tweakimp commented 4 years ago

Ok, but note this:

import math

floor = lambda a, b: a // b

x = 10
y = 3.4

print(type(math.floor(x / y))) # <class 'int'>
print(type(floor(x, y))) # <class 'float'>