Home > front end >  Does this datetime use milliseconds as time unit?
Does this datetime use milliseconds as time unit?

Time:09-17

For example:

import datetime

begin = datetime.datetime.now()

results = process(lines)

end = datetime.datetime.now()

print('Time spent: {}\n'.format(end-begin))

It reports:

Time spent:  0:00:00.005437

Does this mean the process function only take 0.005 millseconds? It seems too little for my code, since it's quite heavy processing. My question is, does the datetime.now() use millsecond or second as time unit?

CodePudding user response:

A datetime.datetime difference results in a datetime.timedelta instance

Its string representation is given by

def __str__(self):
    mm, ss = divmod(self._seconds, 60)
    hh, mm = divmod(mm, 60)
    s = "%d:d:d" % (hh, mm, ss)
    if self._days:
        def plural(n):
            return n, abs(n) != 1 and "s" or ""
        s = ("%d day%s, " % plural(self._days))   s
    if self._microseconds:
        s = s   ".d" % self._microseconds
    return s

The format is so hour:minute:second.microsecond


In your case that makes

  • 5437 microsecond
  • 5 millisecond
  • 0.005437 second

So 200 runs of that will make a second

CodePudding user response:

Refer Official Documentation for more details

https://docs.python.org/3/library/datetime.html

class datetime.time

An idealized time, independent of any particular day, assuming that every day has exactly 246060 seconds. (There is no notion of “leap seconds” here.) Attributes: hour, minute, second, microsecond, and tzinfo.

When I implemented your code I got this

`import datetime

begin = datetime.datetime.now()

end = datetime.datetime.now()

print('Time spent: {}\n'.format(end-begin))`

O/P: Time spent: 0:00:00.000014

So the unit time is in millisecond which is more than enough for processing

  • Related