如何以毫秒精度获得API调用之间的绝对时间差?

2024-09-29 21:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有线程API调用,需要协调API调用时间。你知道吗

应用程序的性质意味着我需要在最大API命中率的基础上运行极薄的边距。你知道吗

目前,我正在使用一个存储上一次API调用时间的共享对象来协调从本地计算机到远程API的API调用时间。你知道吗

为了说明本地计算机和远程API之间的连接速度差异,我使用了一个回退响应测试,如果两个调用过早地命中API,则重新提示API调用,从而导致响应错误。(不过,我真的不喜欢这个解决方案。。。如果你有更好的,请插嘴。)

问题是:

你知道吗时间。时间()并不能在所有平台上提供足够的精度。我需要毫秒精度。你知道吗

你知道吗时钟()据我所知,处理的是过程时间,而不是绝对时间。因此,如果CPU处于不同的负载下,它将根据这些负载差异从绝对时间中分离出来。是这样吗?你知道吗

因此,考虑到这一点,以毫秒精度获得API调用之间的时间差的最佳方法是什么?你知道吗


Tags: 对象api应用程序远程计算机错误时间精度
1条回答
网友
1楼 · 发布于 2024-09-29 21:38:30

精度仍然取决于平台,但是您应该使用timeit.default_timer。你知道吗

docs

Define a default timer, in a platform-specific manner. On Windows, time.clock() has microsecond granularity, but time.time()‘s granularity is 1/60th of a second. On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise. On either platform, default_timer() measures wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing.

相关问题 更多 >

    热门问题