早起闲的没事,做了一个测试,有关Qt string 与java string 性能比较的测试,测试的数据源是RFC 3261的一个报文,把其按照"\r\n"进行解析。分别用了java的String.split方法和QString的split方法,当然这个测试可能比较片面,无法反应真实的2者性能差距,但测试结果也是很有意思
硬件平台 I5 2.3 GHz DDR3 1600MHz
Mac OS 10.7.2
java 1.6.0_33
Qt: 4.7.4
代码如下:
public class myClass
{
public static String recvMsg = "INVITE sip:[email protected] SIP/2.0\r\n" +
"Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds\r\n" +
"Max-Forwards: 70\r\n" +
"To: Bob <sip:[email protected]>\r\n" +
"From: Alice <sip:[email protected]>;tag=1928301774\r\n" +
"Call-ID: [email protected]\r\n" +
"CSeq: 314159 INVITE\r\n" +
"Contact: <sip:[email protected]>\r\n" +
"Content-Type: application/sdp\r\n" +
"Content-Length: 142\r\n";
public static void main(String [] args)
{
System.out.println("main function called");
long startTime = System.currentTimeMillis();
for ( int i = 0 ; i < 10000 * 1000 ; i ++ )
{
String [] result = recvMsg.split("\r\n");
if ( result.length != 10 )
{
System.out.println("not 10 ");
break;
}
}
long endTime = System.currentTimeMillis();
System.out.println("alltime " + (endTime - startTime));
}
}
测试的几次结果如下:
1) 40795 ms
2) 34476 ms
3) 32886 ms
4) 41293 ms
#include <QtCore/QCoreApplication>
#include <QtCore/QTime>
#include <QtCore/QStringList>
#include <QtCore/QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString recvMsg = "INVITE sip:[email protected] SIP/2.0\r\n"\
"Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds\r\n"\
"Max-Forwards: 70\r\n" \
"To: Bob <sip:[email protected]>\r\n" \
"From: Alice <sip:[email protected]>;tag=1928301774\r\n"\
"Call-ID: [email protected]\r\n" \
"CSeq: 314159 INVITE\r\n" \
"Contact: <sip:[email protected]>\r\n" \
"Content-Type: application/sdp\r\n" \
"Content-Length: 142\r\n";
QTime startTime = QTime::currentTime();
for ( int i = 0 ; i < 1000*10000; i ++ )
{
const QStringList & result = recvMsg.split("\r\n"); //为了提高效率,采用引用
if ( result.size() != 11 ) //QString.split会有一个空白字符被解析出来,所以数组内多一个value
{
qDebug()<< "decode error"<<result.size();
break;
}
}
QTime endTime = QTime::currentTime();
qDebug()<<" all time "<< startTime.msecsTo(endTime);
return a.exec();
}
编译为release版本,测试结果如下:
1) 39132 ms
2) 41153 ms
3) 40572 ms
4) 39043 ms
直观上看,QString的性能比java String的性能要差
from PyQt4.QtCore import *
sipMsg = "INVITE sip:[email protected] SIP/2.0\r\n" \
"Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds\r\n" \
"Max-Forwards: 70\r\n" \
"To: Bob <sip:[email protected]>\r\n" \
"From: Alice <sip:[email protected]>;tag=1928301774\r\n" \
"Call-ID: [email protected]\r\n" \
"CSeq: 314159 INVITE\r\n" \
"Contact: <sip:[email protected]>\r\n" \
"Content-Type: application/sdp\r\n" \
"Content-Length: 142\r\n"
if __name__ == "__main__":
import sys
startTime = QTime.currentTime()
count = 1000*1000
while count > 0:
count = count - 1
result = sipMsg.split("\r\n")
if len(result) != 11:
print "error"
endTime = QTime.currentTime()
print startTime.msecsTo(endTime)
最后测试了一下python,速度很快阿
/usr/local/bin/python /t2/main.py
1730