我有一个程序来计算pub-sub模型中对象的延迟.我已将以下函数用于时间戳:
uint64_t GetTimeStamp() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}
Run Code Online (Sandbox Code Playgroud)
延迟测量为发布者和订阅者的时间戳差异.所以,我担心测量的延迟单位.是几秒钟还是几微秒?
我想clock_gettime使用弃用的gettimeofday参考来检查可靠性,但有时会得到奇怪的结果:
#include <stdio.h>
#include <sys/time.h>
#include <iostream>
void clock_gettime_test()
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
long a = tp.tv_nsec;
usleep(250000);
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
long b = tp.tv_nsec;
printf("clock_gettime (%ld - %ld): %lf msec\n", b, a, (b - a)/1000000.0);
}
void gettimeofday_test()
{
struct timeval tv;
gettimeofday(&tv, NULL);
long a = tv.tv_usec;
usleep(250000);
gettimeofday(&tv, NULL);
long b = tv.tv_usec;
printf("gettimeofday (%ld - %ld): %lf msec\n", b, a, (b - a)/1000.0);
}
int main()
{
clock_gettime_test();
gettimeofday_test();
return 0; …Run Code Online (Sandbox Code Playgroud) time命令返回执行命令所用的时间.
如果我在命令调用开始时使用"gettimeofday()"(使用system()),并在调用结束时添加一个,并且有所不同,那么它就不会出现相同的情况.(它的差异也不是很小)
任何人都可以解释这两种用法之间的确切区别是什么,这是为执行呼叫计时的最佳方式?
谢谢.
是否有更多面向对象的替代方法在Linux上使用C++中的gettimeofday()?我喜欢例如能够编写类似于此的代码:
DateTime now = new DateTime;
DateTime duration = new DateTime(2300, DateTime.MILLISECONDS)
DateTime deadline = now + duration;
while(now < deadline){
DoSomething();
delete now;
now = new DateTime()
}
Run Code Online (Sandbox Code Playgroud)
目标是一个嵌入式Linux系统,没有Boost库,但也许有一些容易移植的东西(例如仅使用头文件实现的东西).
我正在尝试将指针传递struct timevals给一个函数,该函数将在C程序中输出两者之间的经过时间.但是,即使我取消引用这些指针,nvcc也会抛出错误"表达式必须具有类类型"(这是一个CUDA程序).以下是main()的相关代码:
struct timeval begin, end;
if (tflag) { HostStartTimer(&begin) };
// CUDA Kernel execution
if (tflag) { HostStopTimer(&begin, &end); }
Run Code Online (Sandbox Code Playgroud)
以及HostStopTimer()的函数定义:
void HostStopTimer(struct timeval *begin, stuct timeval *end) {
long elapsed;
gettimeofday(end, NULL);
elapsed = ((*end.tv_sec - *begin.tv_sec)*1000000 + *end.tv_usec - *begin.tv_usec);
printf("Host elapsed time: %ldus\n", elapsed);
}
Run Code Online (Sandbox Code Playgroud)
导致错误的行是赋值elapsed.我没有太多在C中使用结构的经验,更不用说将结构传递给函数的结构,所以我不确定导致错误的是什么.
我试图在嵌入式 ARM 设备上使用 gettimeofday,但似乎我无法使用它:
gnychis@ubuntu:~/Documents/coexisyst/econotag_firmware$ make
Building for board: redbee-econotag
CC obj_redbee-econotag/econotag_coexisyst_firmware.o
LINK (romvars) econotag_coexisyst_firmware_redbee-econotag.elf
/home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none- eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-gettimeofdayr.o): In function `_gettimeofday_r':
gettimeofdayr.c:(.text+0x1c): undefined reference to `_gettimeofday'
/home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
collect2: ld returned 1 exit status
make[1]: *** [econotag_coexisyst_firmware_redbee-econotag.elf] Error 1
make: *** [mc1322x-default] Error 2
Run Code Online (Sandbox Code Playgroud)
我假设我不能使用 gettimeofday() ?有没有人有任何建议能够告诉经过的时间?(例如,100 毫秒)
我想减去两个gettimeofday实例,并以毫秒为单位显示答案.
这个想法是:
static struct timeval tv;
gettimeofday(&tv, NULL);
static struct timeval tv2;
gettimeofday(&tv2, NULL);
static struct timeval tv3=tv2-tv;
Run Code Online (Sandbox Code Playgroud)
然后将'tv3'转换为毫秒分辨率.
我正在编写一个线程库,在调度线程时,我需要知道它们已经准备了多长时间。每个 Thread 实例都有一个timeval _timeInReady字段,当我将一个实例推送到就绪队列时,我调用了这个函数:
void Thread::startTiming() {
gettimeofday(&_timeInReady, NULL);
}
Run Code Online (Sandbox Code Playgroud)
当我想检查当前_timeInReady值时,我调用:
double Thread::getTimeInReady() const {
return TIME(_timeInReady.tv_sec,_timeInReady.tv_usec);
}
Run Code Online (Sandbox Code Playgroud)
TIME 在哪里#define TIME(a,b) ((a*1000000) + b)这样我就可以得到总时间(以微秒为单位)。
我的问题是,由于某种原因,我在一段时间后检查该字段时得到了疯狂的负值(例如 -10293843)。
有任何想法吗?谢谢!
可能重复,但gettimeofday()在c ++ 11中有什么相同的?
我试图timestamp用微秒获得64位,类似于Java/Python.
Swift中的代码
...
var time:timeval?
gettimeofday(UnsafePointer<timeval>, UnsafePointer<()>) // this is the method expansion before filling in any data
...
Run Code Online (Sandbox Code Playgroud)
目标C中的代码
...
struct timeval time;
gettimeofday(&time, NULL);
...
Run Code Online (Sandbox Code Playgroud)
我一直在试图找到有关UnsafePointer的更多信息以及传递NULL的替代方法,但我可能正在咆哮错误的树.
如果有人知道如何在Swift中使用等效代码,那就太好了.如果有一个很好的解释,它会发生什么,甚至更好!