Skip to content

Instantly share code, notes, and snippets.

@sempr
Created July 4, 2018 05:06
Show Gist options
  • Save sempr/1d3fe23b6de23a302f387239a37eeb55 to your computer and use it in GitHub Desktop.
Save sempr/1d3fe23b6de23a302f387239a37eeb55 to your computer and use it in GitHub Desktop.
RunLeapYear Speed Test
def is_leap_year1(y):
return (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)
def is_leap_year2(y):
return (y % 400 == 0) or (y % 4 == 0 and y % 100 != 0)
def is_leap_year3(y):
return (y % 4 == 0) and (y % 400 == 0 or y % 100 != 0)
def is_leap_year4(y):
return (y % 4 == 0) and (y % 100 != 0 or y % 400 == 0)
def is_leap_year5(y):
return (y % 100 != 0 or y % 400 == 0) and (y % 4 == 0)
def test(f):
for i in xrange(1000, 2000):
f(i)
if __name__ == "__main__":
import time
for idx, f in enumerate([is_leap_year1, is_leap_year2, is_leap_year3, is_leap_year4, is_leap_year5]):
t = time.time()
for i in xrange(10000):
test(f)
print idx, f.__name__, time.time() - t
"""
0 is_leap_year1 2.99913096428
1 is_leap_year2 3.19666790962
2 is_leap_year3 2.52836704254
3 is_leap_year4 2.25670194626
4 is_leap_year5 3.26516008377
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment