Python speed testing - Time Difference - milliseconds

后端 未结 13 1215
小蘑菇
小蘑菇 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 10:51

    You may want to look into the profile modules. You'll get a better read out of where your slowdowns are, and much of your work will be full-on automated.

    0 讨论(0)
  • 2020-11-27 10:52

    The following code should display the time detla...

    from datetime import datetime
    
    tstart = datetime.now()
    
    # code to speed test
    
    tend = datetime.now()
    print tend - tstart
    
    0 讨论(0)
  • 2020-11-27 10:56

    I am not a Python programmer, but I do know how to use Google and here's what I found: you use the "-" operator. To complete your code:

    from datetime import datetime
    
    tstart = datetime.now()
    
    # code to speed test
    
    tend = datetime.now()
    print tend - tstart
    

    Additionally, it looks like you can use the strftime() function to format the timespan calculation in order to render the time however makes you happy.

    0 讨论(0)
  • 2020-11-27 10:56

    Arrow: Better dates & times for Python

    import arrow
    start_time = arrow.utcnow()
    end_time = arrow.utcnow()
    (end_time - start_time).total_seconds()  # senconds
    (end_time - start_time).total_seconds() * 1000  # milliseconds
    
    0 讨论(0)
  • 2020-11-27 10:57

    You could also use:

    import time
    
    start = time.clock()
    do_something()
    end = time.clock()
    print "%.2gs" % (end-start)
    

    Or you could use the python profilers.

    0 讨论(0)
  • 2020-11-27 10:59

    I know this is late, but I actually really like using:

    import time
    start = time.time()
    
    ##### your timed code here ... #####
    
    print "Process time: " + (time.time() - start)
    

    time.time() gives you seconds since the epoch. Because this is a standardized time in seconds, you can simply subtract the start time from the end time to get the process time (in seconds). time.clock() is good for benchmarking, but I have found it kind of useless if you want to know how long your process took. For example, it's much more intuitive to say "my process takes 10 seconds" than it is to say "my process takes 10 processor clock units"

    >>> start = time.time(); sum([each**8.3 for each in range(1,100000)]) ; print (time.time() - start)
    3.4001404476250935e+45
    0.0637760162354
    >>> start = time.clock(); sum([each**8.3 for each in range(1,100000)]) ; print (time.clock() - start)
    3.4001404476250935e+45
    0.05
    

    In the first example above, you are shown a time of 0.05 for time.clock() vs 0.06377 for time.time()

    >>> start = time.clock(); time.sleep(1) ; print "process time: " + (time.clock() - start)
    process time: 0.0
    >>> start = time.time(); time.sleep(1) ; print "process time: " + (time.time() - start)
    process time: 1.00111794472
    

    In the second example, somehow the processor time shows "0" even though the process slept for a second. time.time() correctly shows a little more than 1 second.

    0 讨论(0)
提交回复
热议问题