Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to the client. (We are testing wifi speeds...) it is very crucial that my client program measures how much time it took to send these bytes. I need a "stopwatch" to measure in milliseconds how much time it took to get the 1 byte message back from the server.

My question is, how can I do this? I know that there is time library, but there is no function in there that can help me measure in milliseconds like I need. Thank you

Also I am using Python 3.4 for this project.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
118 views
Welcome To Ask or Share your Answers For Others

1 Answer

you can Use timeit module or use decorator to get execution time of function:

import time
def timer(func):
   def func_wrapper(*args, **kwargs):
        before = time.time()
        call = func(*args, **kwargs)
        after = time.time()
        print '%s function took %0.5f millisecond' % (func.__name__, (after-before)*1000.0)
        return call
   return func_wrapper

@timer
def test():
    return[i**2 for i in range(10000)]


test()

output:

test function took 3.99995 millisecond

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...