系统时间(以毫秒为单位)c++
我有一个带有结构的队列。该结构包括将自身推入队列的确切时间
我有类似的东西来查看系统时间:
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
long callTime=timeinfo->tm_hour*3600+timeinfo->tm_min*60+timeinfo->tm_sec;
q.push( Call( callNum, callTime, callLength ));
问题是当我从队列中弹出下一个结构时,我想计算结构在队列中等待的时间(以毫秒为单位)。
我希望有人能明白我想说的话..:\
I have a queue with structs. This struct includes the exact time of pushing itself into queue
I have something like this to see system time:
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
long callTime=timeinfo->tm_hour*3600+timeinfo->tm_min*60+timeinfo->tm_sec;
q.push( Call( callNum, callTime, callLength ));
The problem is when I pop next struct from the queue, I wanna calculate how much long struct waited in queue in milliseconds.
I hope someone can get what I wanna say.. :\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 gettimeofday,然后减去存储在队列元素中的时间。
gettimeofday()
可以提供以毫秒为单位的时间分辨率。您可以检查此SO链接 了解更多信息。这是假设堆栈中的每个元素也存储它被压入的时间。如果该元素没有它,您可以存储一个包含该元素和时间的结构,或者拥有一个单独的堆栈,仅包含时间。
You can use
gettimeofday
and then subtract the time stored in your queue element.gettimeofday()
can provide time resolution in milliseconds. You can check this SO link for more info.This is assuming that every element in the stack also stores the time that it was pushed in. If the element doesn't have it, you can either store a structure with the element and the time or have a separate stack with the times alone.
您需要使用函数
gettimeofday()
而不是time()
。前者支持亚毫秒精度,而后者仅精确到最接近的秒。You need to use the function
gettimeofday()
rather thantime()
. The former supports sub-millisecond precision, while the latter is only accurate to the nearest second.gettimeofday 是一个答案,但如果您只想检查您的项目在队列中的时间,clock_gettime() 可能更合适。
例如,您可以使用 CLOCK_MONOTONIC (假设我们不计算秒):
当然,clock_gettime 不是 C++ 的一部分,它是 POSIX.1-2008 的一部分
gettimeofday is a answer, but if you want just check how long your item was in queue clock_gettime() could be more suitable.
For example, you could use CLOCK_MONOTONIC (suppose we do not count sec):
And, of course, clock_gettime is not part of C++, it is part of POSIX.1-2008