QString和QByteArray的效率等比较
来源:互联网 发布:彩票关注网站源码 编辑:程序博客网 时间:2024/04/28 01:33
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).
See also operator+=(), prepend(), and insert().
See also append() and prepend().
See also append() and prepend().
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.
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.
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.
测试
QTime t;
t.start();
QByteArray ba;
for(int n=0;n<1000000;n++)
{
ba+="string";
}
qDebug()<<"milliseconds elapsed:"<<t.elapsed();
结果: 100万次字符串连接,用时57毫秒
结论: QByteArray的字符串连接速度会比QString更快,提升一个数量级。
结果: 100万次字符串连接,用时57毫秒
QTime t;
t.start();
QString ba;
for(int n=0;n<1000000;n++)
{
ba+="string";
}
qDebug()<<"milliseconds elapsed:"<<t.elapsed();
结果:100万次字符串连接,用时641毫秒结论: QByteArray的字符串连接速度会比QString更快,提升一个数量级。
0 0
- QString和QByteArray的效率等比较
- QString和QByteArray的区别
- QString和QByteArray的区别
- QT:QString、QByteArray和char *的转换
- QT:QString、QByteArray和char *的转换
- QT:QString、QByteArray和char *的转换
- char* QByteArray 和 QString之间的转换
- QT:QString、QByteArray和char *的转换
- QT:QString、QByteArray和char *的转换。
- QString, QByteArray,和 QVariant
- QString和QByteArray
- Qt中的QString和QByteArray
- Qt中的QString和QByteArray
- Qt中的QString和QByteArray
- qt中的QString和QByteArray
- Qt中的QString和QByteArray
- Qt中的QString和QByteArray
- Qt中的 QString和QByteArray
- UIImagePickerController调取系统相册图片
- WifiManager-Wifi Control App-Note
- java 获取Connection
- delphi基本语法(摘自博主:沈金强)
- math.h中一些常用函数
- QString和QByteArray的效率等比较
- linux中sed命令详解
- 自定义AlertDialog,仿Ios效果
- 一个简单的扑克牌小程序
- 有默认参数的函数
- Android Studio获取数字签名
- Kmens kmeans++及 knn算法的比较
- <sdut-ACM>数据结构实验之链表一:顺序建立链表
- 统一设置push进来的控制器的左上角的返回按钮