【问题标题】:How to show messages at real time during peak volume如何在高峰期实时显示消息
【发布时间】: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


【解决方案1】:

您无法匹配计时器。

没有可靠的方法来确定函数调用在同一毫秒内发生的时间。您的操作系统可能需要暂停程序的执行以执行具有更高优先级的调用(在这种情况下,例如控制台输出)。

【讨论】:

  • 不,先生 - 绝对有可能在同一毫秒内进行多个函数调用!
  • 在多个线程中,是的,在理论上,但时间。您将如何同时或在一毫秒内执行两条 cpu 指令?其他进程也可能需要资源?这就是您需要定时在同一毫秒内进行不同呼叫的时间,并且没有可靠的方法
  • 不,即使在同一个线程中。 3GHz 处理器每 纳秒 可以执行 9 条指令;这为每毫秒执行数千(甚至数万或数十万)次操作留下了很大的空间。做一些算术验证,或者编写一个紧密的 C 循环,看看你可以在 1 毫秒内完成多少个函数调用......
  • @maerics:不在线程范围内。您的线程可能会被关闭。你不能保证这样的事情。
  • 对,但是在你的程序中计时是诀窍,我会更新我的答案 - 完成
【解决方案2】:

您可能正在查看线程量。即使调度程序决定让你的线程立即再次运行,它仍然必须暂停它来决定——它可能会决定允许其他线程运行。没有办法真正让你的程序在这种精度范围内运行。当控制台 I/O 阻塞时尤其如此,这会大大浪费您的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多