QString和QByteArray的效率等比较

http://user.qzone.qq.com/892054726/blog/1435321165


The QString class provides a Unicode character string.

Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data . This also helps reduce the inherent overhead of storing 16-bit characters instead of 8-bit characters.
In addition to QString, Qt also provides the QByteArray class to store raw bytes and traditional 8-bit '\0'-terminated strings. For most purposes, QString is the class you want to use . It is used throughout the Qt API, and the Unicode support ensures that your applications will be easy to translate if you want to expand your application's market at some point. The two main cases where QByteArray is appropriate are when you need to store raw binary data , and when memory conservation is critical (like in embedded systems).

QString & QString::?append(const QString & str) Appends the string str onto the end of this string.
Example:
QString x = "free"; QString y = "dom";
x.append(y); // x == "freedom" This is the same as using the insert() function:
x.insert(x.size(), y); The append() function is typically very fast (constant time), because QString preallocates extra space at the end of the string data so it can grow without reallocating the entire string each time.
See also operator+=(), prepend(), and insert().


QString & QString::?operator+=(const QString & other) Appends the string other onto the end of this string and returns a reference to this string.
Example:
QString x = "free"; QString y = "dom"; x += y; // x == "freedom" This operation is typically very fast (constant time), because QString preallocates extra space at the end of the string data so it can grow without reallocating the entire string each time.
See also append() and prepend().



QByteArray & QByteArray::?operator+=(const QByteArray & ba) Appends the byte array ba onto the end of this byte array and returns a reference to this byte array.
Example:
QByteArray x("free"); QByteArray y("dom"); x += y; // x == "freedom" Note: QByteArray is an implicitly shared class. Consequently, if this is an empty QByteArray, then this will just share the data held in ba. In this case, no copying of data is done, taking constant time. If a shared instance is modified, it will be copied (copy-on-write), taking linear time.
If this is not an empty QByteArray, a deep copy of the data is performed, taking linear time.
This operation typically does not suffer from allocation overhead, because QByteArray preallocates extra space at the end of the data so that it may grow without reallocating for each append operation.
See also append() and prepend().

测试QTime t; t.start(); QByteArray ba; for(int n=0; n<1000000; n++) { ba+="string"; }
qDebug()<<"milliseconds elapsed:"<
结果:100万次字符串连接,用时57毫秒

QTime t; t.start(); QString ba; for(int n=0; n<1000000; n++) { ba+="string"; }
qDebug()<<"milliseconds elapsed:"<结果:100万次字符串连接,用时641毫秒

结论: QByteArray的字符串连接速度会比QString更快,提升一个数量级。

【QString和QByteArray的效率等比较】

    推荐阅读