我在大学接受了基础C ++讲座,而且我真的不知道如何为实时应用程序优化这行代码(我每5 ms调用一次这个函数用于UDP数据包解码)。
我想提供一些有关此代码的提示:
提前感谢您阅读和回答。
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];
}
}
}
}
}
}