Python speed testing - Time Difference - milliseconds

后端 未结 13 1214
小蘑菇
小蘑菇 2020-11-27 10:41

What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I\'m not sure I understand the timedelta thing.

相关标签:
13条回答
  • 2020-11-27 11:02

    Since Python 2.7 there's the timedelta.total_seconds() method. So, to get the elapsed milliseconds:

    >>> import datetime
    >>> a = datetime.datetime.now()
    >>> b = datetime.datetime.now()
    >>> delta = b - a
    >>> print delta
    0:00:05.077263
    >>> int(delta.total_seconds() * 1000) # milliseconds
    5077
    
    0 讨论(0)
提交回复
热议问题