Skip to content

Instantly share code, notes, and snippets.

@mplanchard
Created March 1, 2017 17:51
Show Gist options
  • Save mplanchard/9862cb294f0c6b5f8c66839535f287e3 to your computer and use it in GitHub Desktop.
Save mplanchard/9862cb294f0c6b5f8c66839535f287e3 to your computer and use it in GitHub Desktop.
Datetime New Obj vs Replace
from datetime import datetime
from timeit import timeit
iterations = int(1e6)
def method_one():
now = datetime.now()
hour = datetime(now.year, now.month, now.day, now.hour)
return hour
def method_two():
now = datetime.now().replace(minute=0, second=0, microsecond=0)
hour = now.replace(minute=0, second=0, microsecond=0)
return hour
res_one = timeit(method_one, number=iterations)
res_two = timeit(method_two, number=iterations)
print('Total time for %s iterations:', iterations)
print('')
print('Method one: ')
print('- %s' % res_one)
print('- avg: {:.10E} s'.format(res_one / iterations))
print('')
print('Method two: ')
print('- %s' % res_two)
print('- avg: {:.10E} s'.format(res_two / iterations))
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment