2011-08-21 20 views
-1

我正試圖找到每秒顯示大量消息的最佳方法。我開發了以下程序,該程序可生成隨機數據,用於模擬必須實時顯示並且沒有任何顯示時間的數據流。如何在峯值期間實時顯示消息

該程序顯示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(""); 
     } 
    } 
} 
+0

@DeadMG:err,對,我的壞... – maerics

回答

0

你無法比擬的計時器。

沒有可靠的方法來使函數調用在同一毫秒內發生。您的操作系統可能需要暫停程序的執行才能執行具有更高優先級的調用(例如,在此情況下爲控制檯輸出)。

+0

沒有先生 - 這絕對是可能的在同一個毫秒內進行多個函數調用! – maerics

+0

在多個線程中,是的,理論上,但時間。你將如何在同一時間或毫秒內完成兩個cpu指令?其他進程可能也需要資源?那就是你需要在同一毫秒內得到定時做不同的調用,並且沒有可靠的方法 –

+0

不會,即使在同一個線程中也是如此。 3GHz處理器可以執行每個*納秒* 9條指令;這爲每毫秒數千次(甚至幾十或幾十萬次)的操作留下了很多空間。做一些算術驗證,或者寫一個C的緊密循環,看看你能在1ms內完成多少次函數調用...... – maerics

0

你可能正在看線程量子。即使調度程序決定讓線程立即再次運行,它仍然需要暫停它來決定 - 它可能會決定允許其他線程運行。沒有辦法真正讓你的程序以這種精度運行。當控制檯I/O被阻塞時,尤其如此,這將平息你的時間。

相關問題