【发布时间】:2019-09-12 09:01:49
【问题描述】:
我想测量通过 TCP 发送和返回响应的时间,我想知道 htons 函数从小端到大端的转换需要多少时间。
【问题讨论】:
-
无实际用途。
-
@Shawn 不是没有,而是无穷小;)
-
@nada 尝试阅读 Shawn 评论中的以下文字
我想测量通过 TCP 发送和返回响应的时间,我想知道 htons 函数从小端到大端的转换需要多少时间。
【问题讨论】:
关于我们技术时代的普通硬件:
通过 TCP 发送和接收数据是一个在几毫秒到几分钟的时间范围内运行的过程。
在内存中转换几个字节的字节顺序是一个在几纳秒的时间尺度上运行的过程。
我想知道通过 htons 函数从 little-endian 转换到 big-endian 需要多少时间
可以忽略不计,相差 6 个数量级。
【讨论】:
您可以使用 high_resolution_clock 测量函数调用前后的时间,然后您可以计算持续时间。
#include <chrono>
high_resolution_clock::time_point t1 = high_resolution_clock::now();
functionToMeasure();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(t2 - t1).count();
【讨论】:
htons 立即进行转换。