我正試圖找到每秒顯示大量消息的最佳方法。我開發了以下程序,該程序可生成隨機數據,用於模擬必須實時顯示並且沒有任何顯示時間的數據流。如何在峯值期間實時顯示消息
該程序顯示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:err,對,我的壞... – maerics