How can I implement a stopwatch in my socket Python program

雨燕双飞 提交于 2019-12-13 10:29:08

问题


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:


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


来源:https://stackoverflow.com/questions/40143164/how-can-i-implement-a-stopwatch-in-my-socket-python-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!