如何在我的套接字 Python 程序中实现秒表 [英] How can I implement a stopwatch in my socket Python program

查看:35
本文介绍了如何在我的套接字 Python 程序中实现秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 创建一个非常简单的 ping 程序,它将向本地服务器发送 16-64 字节的信息,一旦服务器收到所有字节,它将向客户端发送回 1 字节的消息.(我们正在测试 wifi 速度......)我的客户端程序测量发送这些字节所需的时间非常重要.我需要一个秒表"以毫秒为单位测量从服务器返回 1 字节消息所需的时间.

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

我也在这个项目中使用 Python 3.4.

Also I am using Python 3.4 for this project.

推荐答案

你可以使用 timeit 模块或者使用 decorator 来获取函数的执行时间:

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()

输出:

test function took 3.99995 millisecond

这篇关于如何在我的套接字 Python 程序中实现秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆