【发布时间】:2011-08-21 22:12:48
【问题描述】:
我正在努力寻找每秒显示大量消息的最佳方式。我开发了以下程序,该程序生成随机数据,该数据模拟必须实时显示且没有延迟的流。
程序显示 4 列:真实时间戳、数据时间戳、延迟时间和消息 ID。
一个例子:
00:00:00.002000 00:00:00.000000 00:00:00.000000 #1
00:00:00.592034 00:00:00.585000 00:00:00.585000 #2
00:00:01.653095 00:00:01.642000 00:00:01.057000 #3
00:00:01.692097 00:00:01.675000 00:00:00.033000 #4
00:00:01.698097 00:00:01.675000 00:00:00.000000 #5
00:00:01.698097 00:00:01.675000 00:00:00.000000 #6
00:00:01.698097 00:00:01.675000 00:00:00.000000 #7
00:00:01.698097 00:00:01.675000 00:00:00.000000 #8
00:00:01.698097 00:00:01.675000 00:00:00.000000 #9
00:00:01.698097 00:00:01.675000 00:00:00.000000 #10
...
例如,第 4 行在 1.675 秒处“收到”,它与第 3 行相比有 0.033 秒的延迟,而这条消息实际上是在 1.692097 处显示的。第一列和第二列应尽可能靠近。但是,当有数据选择时,计时器会发生分歧。运行程序时,您会注意到第 2 列是如何粘滞的,因为在同一毫秒显示的消息是逐行绘制的,而不是一次全部显示。我不知道是硬件限制还是实现不好,但测试最后显示第一列的时间如何比第二列的时间高几倍。我知道计算和数据显示需要一些时间,但我认为差异太大。 如何匹配两个计时器?如果我不能,我怎样才能让它们尽可能接近?
非常感谢您的宝贵时间,
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <time.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
using namespace std;
using namespace boost::posix_time;
int main(int argc, char* argv[])
{
srand (time(NULL));
int rmil, num=0; //Integers for generating random milliseconds and counting messages
time_duration snapshot, sum = milliseconds(0); //Sum of total message delais and its holding value
struct message
{
time_duration delay;
string print;
} m;
list<message> mlist; //List of messages
//Simulating 30 seconds of data with peaks of volume.
//The first message is at time 0
m.delay = milliseconds(0); num++;
m.print = to_simple_string(sum)+" 00:00:00.000000 #"+boost::lexical_cast<std::string>(num);
mlist.push_back(m);
while(sum<seconds(30)) //Generating 30 seconds of data
{
if(rand()%100<10) // Probability to have a peak data volume
{
snapshot = sum;
while(sum<snapshot+seconds(1)) //Generating messages for 1 second
{
rmil = rand() % 100; //0.050 second delay between packs of messages
int mpm = rand() % 150; //Num of Message per millisecond
m.delay = milliseconds(rmil);
num++; sum += milliseconds(rmil);
m.print = to_simple_string(sum)+" "+to_simple_string(m.delay)+" #"+boost::lexical_cast<std::string>(num);
mlist.push_back(m);
for(int n=0;n<mpm;n++) //Adding messages at the same millisecond
{
m.delay = milliseconds(0); num++;
m.print = to_simple_string(sum)+" 00:00:00.000000 #"+boost::lexical_cast<std::string>(num);
mlist.push_back(m); //Push message to the list
}
}
}
else
{
rmil = rand() % 2000; //1 second delay (average) between messages, no peak volume
m.delay = milliseconds(rmil);
num++; sum += milliseconds(rmil);
m.print = to_simple_string(sum)+" "+to_simple_string(m.delay)+" #"+boost::lexical_cast<std::string>(num);
mlist.push_back(m);
}
}
//Displaying messages with delay
list<message>::iterator it = mlist.begin();
stringstream scrmsg;
ptime ltime = microsec_clock::local_time(); //Record the local time
while(it!=mlist.end())
{
if((*it).delay > boost::posix_time::milliseconds(0))
{
boost::this_thread::sleep((*it).delay);
cout << to_simple_string(microsec_clock::local_time()-ltime) << " " <<(*it).print << endl;
it++;
}
else //Group the messages at the same millisecond
{
while((*it).delay == boost::posix_time::milliseconds(0))
{
scrmsg << to_simple_string(microsec_clock::local_time()-ltime) << " " << (*it).print << endl;
it++;
}
cout << scrmsg.str();
scrmsg.str("");
}
}
}
【问题讨论】:
-
@DeadMG:错,对,我的错……
标签: c++ string real-time simulation