问题标签 [gettimeofday]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
7865 浏览

c++ - 如何减去两个 gettimeofday 实例?

我想减去两个 gettimeofday 实例,并以毫秒为单位给出答案。

这个想法是:

然后将“tv3”转换为毫秒分辨率。

0 投票
2 回答
853 浏览

c - Slow initial timing results using gettimeofday - worse under RHEL6 Server

I am using gettimeofday() to time a simple matrix multiply example, but I'm getting results that are close to twice too long initially. On a RHEL6 Server machine, I'm getting "bad" timing results for up to nearly 1 second (~65 individual timings in this example). All our other machines are RHEL5 Workstation boxes and this code works much better on them; I only get a couple of "bad" results initially (for first ~20 milliseconds).

From posts on this site, I think this probably has something to do with the OS process scheduler. If I uncomment the first "for" statement below (thereby inserting an initial busy loop by repeatedly initializing the matrices a, b and c), I get zero "bad" results under both RHEL5 Workstation and RHEL6 Server. Alternatively, if I uncomment the sleep statement, I get ALL "bad" timing results for both RHEL5 & RHEL6.

For some reason, my process is starting up with only about half the access to the CPU initially, then it gets "full" access to the CPU as long as the process stays busy. If it "sleeps" and then resumes timing, it again is temporarily only getting about half the full access to the CPU.

Nothing else is happening on the machine (X is not running). I have tried "chrt" to control the priority of the process, but that changed nothing. I've verified this occurs with both GCC 4.4.6 and ICC 12.1.0. I've tried "nice" as well.

Here's the code:

and here are the results:

I experience the problem regardless of optimization level (-O0, -O1, -O2, etc.).

Does anyone know anything about how scheduling is done under RHEL6 Server? Is it so very different from RHEL5 Workstation? I presume the difference I'm seeing is more a function of the fact that one box is a server edition of RHEL and the other is a workstation edition (rather than the difference between release 5 vs. 6). Is there some simple way to reduce this effect under RHEL6 Server and make it act more like the RHEL5 Workstation boxes?

Any ideas? Thanks.

0 投票
3 回答
1576 浏览

c - 使用 timeval 结构和 gettimeofday 的奇怪错误——因为 #define 中的分号

我收到了一些奇怪的编译错误。这是一个家庭作业(帮助是好的)。这个想法是实现一个程序来测试用户每秒点击一次“输入”的能力。我应该使用 gettimeofday 来获取每个“输入”的一些时间值,然后找出平均时间和标准偏差......我试图通过检查标准输入的 '\n' 来做到这一点,然后如果是的,使用 gettimeofday 填充 timeval 结构,然后将所述结构存储在数组中以供以后使用...

我在编译(gcc -Wextra homework1.c)时遇到的错误是:

我不明白为什么会出现前两个语法错误,然后我不明白为什么在“main”开头明确声明“entry_array”时未声明它。建议?

我觉得我因为不知道如何使用 timeval 结构而被烧毁......最初我尝试像使用任何其他结构一样全局定义 struct timeval,但是在覆盖 struct timeval 的定义时遇到错误。 .这是因为它是在“sys/time.h”库中定义的吗?

这是代码:

0 投票
1 回答
827 浏览

c++ - C++:fmodf() & fmod() - 奇怪的结果?

所以,我需要一种方法来获得当天的第二天,所以我搞砸了fmod()& gettimeofday()(Mac OSX)。然而,我在此过程中遇到了一些奇怪的结果:

输出:

fmodf: 5760
fmod: 5699.17
fmodf: 5760
fmod: 5700.17
fmodf: 5760
fmod: 5701.17
fmodf: 5760 fmod
: 5702.17 fmodf: 5760 fmod: 5703.17 fmodf: 5760 fmod: 5704.17 fmodf75.7 ...






那么,为什么fmodf()版本每次都给我相同的输出,而fmod()版本给出了预期的结果(sleep()调用后改变)?我在文档中遗漏了什么吗?

0 投票
3 回答
5223 浏览

c++ - gettimeofday 返回一个负值

我正在编写一个线程库,并且在调度线程时我需要知道它们已经准备好多长时间。每个 Thread 实例都有一个timeval _timeInReady字段,当我将实例推送到就绪队列时,我调用此函数:

当我想检查_timeInReady我调用的当前值时:

时间在哪里#define TIME(a,b) ((a*1000000) + b)所以我得到了以微秒为单位的总时间。

我的问题是,由于某种原因,当我在一段时间后检查该字段时,我得到了疯狂的负值(例如 -10293843)。

有任何想法吗?谢谢!

0 投票
2 回答
1164 浏览

c - gettimeofday/settimeofday 用于使函数看起来不需要时间

我有一个辅助功能,可以执行一些非常昂贵的操作。

我正在尝试分析算法的主要部分,但这个辅助函数在内部被调用了很多。因此,测量的时间考虑了辅助功能的时间。

为了解决这个问题,我决定设置和恢复时间,使辅助功能看起来是瞬时的。我定义了以下宏:

. . . 并将它们用作辅助函数的第一行和最后一行。但是,出于某种原因,辅助功能的开销仍然包括在内!

所以,我知道这是一个混乱的解决方案,所以我继续前进,但我仍然很好奇为什么这个想法不起作用。有人可以解释为什么吗?

0 投票
2 回答
957 浏览

perl - gettimeofday 时差偶尔为负

我正在用 perl 编写一个面向对象的 OpenGL 框架,当我测量每帧之间的 DeltaTime 时遇到了一个奇怪的问题。增量时间似乎每隔一段时间就会变为负数(每 ~.5 秒)。我不确定这是 gettimeofday 的问题,还是 GLUT 如何调用我的回调的问题,但这很烦人,因为它使我的精灵的运动每半秒略微跳跃一次。

这是我的过剩主循环功能:

这是输出:

哇!我们刚刚遇到了时间悖论!此函数上次调用 -0.017144 9184417725 秒前...当前时间:1340196716.27624,上次调用:1340196716.29339 我们已经在 0.482785940170288 秒前遇到此错误。

哇!我们刚刚遇到了时间悖论!此函数上次调用 -0.013265 84815979 秒前...当前时间:1340196716.74632,上次调用:1340196716.75959 我们已经在 0.470081090927124 秒前遇到此错误。

哇!我们刚刚遇到了时间悖论!此函数上次调用 -0.011317 9683685303 秒前...当前时间:1340196717.21836,上次调用:1340196717.22968 我们已经在 0.472035884857178 秒前遇到此错误。

哇!我们刚刚遇到了时间悖论!此函数上次调用 -0.015201 0917663574 秒前...当前时间:1340196717.68649,上次调用:1340196717.70169 我们已经在 0.468127012252808 秒前遇到此错误。

0 投票
2 回答
2313 浏览

c++ - 'clock_gettime' 和 'gettimeofday' 的奇怪计时结果

我想检查 的可靠性clock_gettime,使用不推荐gettimeofday的作为参考,但有时会得到奇怪的结果:

构建和运行,我有时会得到正确的结果:

但有时,我没那么幸运:

甚至:

我在 i686 和 amd64 上都试过了,得到了类似的结果。我也试过nanosleep了,结果同样悲惨。

0 投票
2 回答
1210 浏览

c++ - 用 Windows 的时间函数喂 srand()

我一直在寻找下面代码补丁的确切 Win 实现。我不是一个优秀的程序员,我的代码最初是在 UNIX 中,但我必须尽可能少地修改它以用于 Win。我从一些谷歌搜索中了解到,主要问题是没有 Win 等效项的“gettimeofday”。但是,我想尽可能地保留相同的程序结构。

0 投票
2 回答
4830 浏览

c - 从 gettimeofday 开始的时间没有按照应有的方式更新

反复运行此代码生成的二进制文件,我倾向于得到相同的时间值:

这种情况一直发生到几秒钟后,我得到一个更新的时间值。