user8249966
user8249966

Reputation:

How can I print elapsed time in ms using python

I am able to compute elapsed time, but I dont know how to print results in ms (i need integer, like this: 20ms, 30ms..)

import datetime

start_time = datetime.datetime.now()
print 'some long procedure'
elapsed = datetime.datetime.now() - start_time
print int(elapsed).strftime("%s")) * 1000 #<------- not working

Upvotes: 2

Views: 7481

Answers (1)

Thierry Lathuille
Thierry Lathuille

Reputation: 24234

The total_seconds method of datetime.timedelta objects returns the number of seconds, as a float, so it includes the fractions of second - see timedelta.total_seconds.

So, you just have to multiply it by 1000 to convert it to milliseconds, and keep the integer part.

import datetime

start_time = datetime.datetime.now()
print 'some long procedure'
elapsed = datetime.datetime.now() - start_time

print(int(elapsed.total_seconds()*1000))

Upvotes: 5

Related Questions