1

WHy would I choose to use Python's time vs datetime (and also mxDateTime) when either way you can get the same result? For example,

time.localtime()[7]
datetime.date.today().timetuple()[7]

yields the same, mxDateTime can also produce same result.

PythonAnywhere
  • 309
  • 1
  • 3
  • 11

2 Answers2

2

You can check which one is faster by using timeit package in python.

import sys
import timeit
import datetime
import time

def create_datetime():
    return datetime.date.today().timetuple()

def create_time():
    return time.localtime()

if __name__ == '__main__':
    print(timeit.timeit("create_datetime()", setup="from __main__ import create_datetime"))
    print(timeit.timeit("create_time()", setup="from __main__ import create_time"))

It prints:

3.7883488804446963
0.4524141759797713

So, it looks like time.localtime() is better than datetime.date.today().

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
0

I think, from my knowledge, you can use any of the two. It just depends which module you'd be using most, so you don't have unneeded functions lying around.

JohnyNich
  • 1,221
  • 3
  • 14
  • 23