Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

How can I implement a stopwatch in my socket Python program

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...