Search code examples
pythonmongodbperformance-testinglatency

Convert execution time to milliseconds: time.time()


How can I convert the execution time to milliseconds. I already multiplied the start and end time to 1000.

I used time.time()

Execution Time

Result: ('Start time: ', 1596465418538.365)

Remove.IntNonIdUniqueIndex

('End time: ', 1596465418538.399)

('Execution time: ', 3.409385681152344e-05)


Solution

  • time.time() basic unit is second. It's enough to multiply the difference between end and start by 1000 to get the milliseconds.

    import time
    
    start = time.time()
    time.sleep(1)
    end = time.time()
    d = end - start
    print(f'executed in {d} seconds or {d*1000} milliseconds')
    
    executed in 1.003673791885376 seconds or 1003.673791885376 milliseconds