user2315898
user2315898

Reputation:

Converting seconds to milliseconds/microseconds in Python

I'm trying to convert seconds to milliseconds (or microseconds) without dividing by 1,000 or 1,000,000. The reason I'm doing it is because I don't want the decimal point to be shown. Is there a way of showing milliseconds or microseconds without the decimal point and without dividing by 1,000 or 1,000,000?

Here's my code with method execution timing:

from random import randint
list = [1]*1000

for i in range (1, 1000):
    list[i] = randint(1,100)

def insertion(list):
    for index in range(1,len(list)):
        value = list[index]
        i = index - 1
        while i>=0 and (value < list[i]):
            list[i+1] = list[i] 
            list[i] = value
            i = i - 1

def test():
    start = time.clock()
    insertion(list)
    elapsed = (time.clock() - start)
    print "Time taken for insertion = ", elapsed

if __name__ == '__main__':
    import time
    test()

Upvotes: 7

Views: 32065

Answers (2)

Ian Gustafson
Ian Gustafson

Reputation: 172

I think your question may be missing something. To convert an amount of seconds to milliseconds, you can simply multiply (not divide) by 1000.

If it's 52 seconds:

52 * 1000

This would return the integer 52000.

Upvotes: 6

kindall
kindall

Reputation: 184091

Sure! To convert seconds to milliseconds or microseconds, you multiply by the number of the desired divisions in a second (e.g. 1000 for milliseconds). No division needed!

If you don't want the decimal places, use round() or int() on the result.

Upvotes: 12

Related Questions