实时应用程序的代码优化C ++

时间:2017-04-03 11:28:51

标签: c++ optimization

我在大学接受了基础C ++讲座,而且我真的不知道如何为实时应用程序优化这行代码(我每5 ms调用一次这个函数用于UDP数据包解码)。

我想提供一些有关此代码的提示:

  • 如何提高效率(及时处理而不是记忆)? (在UDPmanager类中声明变量,使用静态变量,......?)
  • 我正在使用openFrameworks来执行此项目。使用核心C ++函数会更好吗?
  • 关于我的编码方式的任何提示

提前感谢您阅读和回答。

    void UDPmanager::readPacket() { 

    udpConnection.Receive(udpMessage,100000);  
    string message=udpMessage; 
    static int count = 0;


    if(message!="")
    {
        vector<string> strSensor = ofSplitString(message,"[/c]");

         vector<string> strQoS = ofSplitString(strSensor[0],"|");

         unsigned int receivedNumber = atoi(strQoS[0].c_str());


       QoS_estimator(receivedNumber,message.length(),atoi(strQoS[1].c_str()));


            if(receivedNumber<=previousPacketNumber)
            {
                sleep(1);
                return; // No need to use this packet. (= means it was duplicated in the network, < means it doesn't arrived in order)
            }

        previousPacketNumber=receivedNumber;
        strSensor.erase(strSensor.begin());


        cout << "loss : " << packetLoss <<  endl;
        cout << "counter : " << count++ <<  endl;
        cout << "receivedNumber : " << receivedNumber <<  endl;



        if (strSensor.size()!=sensors.size())
        {
            ofLog() << "The number of sensors changed in the system !" << endl;
        }


        for(unsigned int j =0 ; j<strSensor.size();j++)
            // For each sensors start decoding
        {


             vector<string> strData = ofSplitString(strSensor[j],"[/p]");

        for(size_t i=0; i<strData.size() ; ++i)
        {
            static vector<string> value = ofSplitString(strData[i],"|");

            if (value[0] == "[/i]") // Duplicate initialization frame
            {
                break;
            }


            if (value[0] == "POS")
            {
                sensors[j].touchpadPosition.x = (float)atoi(value[1].c_str())/10000.0;
                sensors[j].touchpadPosition.y = (float)atoi(value[2].c_str())/10000.0;
            }

            else
            {
                if(value[0] == "TOUCH")
                {
                        for(unsigned int i=0;i<sensors[j].data.size();i++)
                        {
                            sensors[j].data[i].touch = (atoi(value[i+1].c_str()))==1;
                        }
                }

                else if(value[0] == "TTHS")
                {
                    for(unsigned int i=0;i<sensors[j].data.size();i++)
                    {
                        sensors[j].data[i].tths = atoi(value[i+1].c_str());
                    }
                }
                else if(value[0] == "RTHS")
                {
                    for(unsigned int i=0;i<sensors[j].data.size();i++)
                    {
                        sensors[j].data[i].rths = atoi(value[i+1].c_str());
                    }
                }
                else if(value[0] == "FDAT")
                {
                    for(unsigned int i=0;i<sensors[j].data.size();i++)
                    {
                        sensors[j].data[i].fdat = atoi(value[i+1].c_str());
                    }
                }
                else if(value[0] == "BVAL")
                {
                    for(unsigned int i=0;i<sensors[j].data.size();i++)
                    {
                        sensors[j].data[i].bval = atoi(value[i+1].c_str());
                    }
                }
                else if(value[0] == "DIFF")
                {
                    for(unsigned int i=0;i<sensors[j].data.size();i++)
                    {
                        sensors[j].data[i].diff = atoi(value[i+1].c_str());
                    }
                }
                else {
                     ofLogError() << "Unrecognized key: " << value[0];
                }



            }

        }


        }



    }
}

0 个答案:

没有答案