I am getting large volume data over TCP. There are 2 type of XML packets in data. I need to process it as fast as possible.
<?xml version="1.0" encoding="UTF-8"?><xsi:Event> .... [dynamic length data] .... </xsi:Event>
and
<?xml version="1.0" encoding="UTF-8"?><ChannelHeartBeat xmlns="http://schema.broadsoft.com/xsi"/>
Sometime packets doesn't have xml declaration.
This is old code.
char c = (char)streamReader.Read();
sb.Append(c);
if(sb.ToString().EndsWith("</xsi:Event>",StringComparison.OrdinalIgnoreCase))
{
....
sb.Clear();
}
if(sb.ToString().EndsWith("<ChannelHeartBeat xmlns=\"http://schema.broadsoft.com/xsi\"/>", StringComparison.OrdinalIgnoreCase))
{
....
sb.Clear();
}
ToString()
was taking 26% of CPU time in above code.
Below is optimized code. It enhanced performance by 30%
char c = (char)streamReader.Read();
sb.Append(c);
n++;
if (n > 60)
{
if (c == '>')
{
if (n < 105)
{
string temp = sb.ToString();
if (temp.EndsWith("<ChannelHeartBeat xmlns=\"http://schema.broadsoft.com/xsi\"/>", StringComparison.OrdinalIgnoreCase))
{
sb.Clear();
n = 0;
}
}
if (n > 700)
{
string temp = sb.ToString();
if (temp.EndsWith("</xsi:Event>", StringComparison.OrdinalIgnoreCase))
{
sb.Clear();
n = 0;
}
}
}
}
}
ToString()
is now taking 8% of CPU time.
I want to optimize code further. Any suggestion is welcome.
Thanks in advance.