How can I implement a stopwatch in my socket Pytho

2019-09-19 10:24发布

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.

1条回答
时光不老,我们不散
2楼-- · 2019-09-19 10:38

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
查看更多
登录 后发表回答