effiently convert qstring to std::string or char
-
If I'm correct then QString::toStdString() create new copy the underlying string no ? or does it not?
What is most efficient way of converting QString to std::string or char* ? I won't need QString after conversion. I am going to receive large QString's from QML/JS and I don't wish to have two copies of same string in form of QString and std::string.
-
@noone said in effiently convert qstring to std::string or char:
but is there any way to reuse the existing underlying data of QString ?
No, how would that work? QString and std::string are two completely different classes with their own internal memory handling. Also, QString uses UTF16 internally as far as I know, std::strinig does not. I'm wondering why you care so much? Do you experience any problems because of that (like memory consumption)?
You can see what toStdString() does here: https://code.woboq.org/qt5/include/qt/QtCore/qstring.h.html#_ZNK7QString11toStdStringB5cxx11Ev
inline std::string QString::toStdString() const { return toUtf8().toStdString(); }
toUtf8() returns a QByteArray and toStdString from QByteArray does:
inline std::string QByteArray::toStdString() const { return std::string(constData(), length()); }
-
@noone said in effiently convert qstring to std::string or char:
QString::toStdString() create new copy the underlying string no ? or does it not?
Sure it does.
If you don't need both copies then don't keep the QString. You can also assign an empty string to your QString after conversion to std::string, so the QString frees the memory. -
@noone said in effiently convert qstring to std::string or char:
but is there any way to reuse the existing underlying data of QString ?
No, how would that work? QString and std::string are two completely different classes with their own internal memory handling. Also, QString uses UTF16 internally as far as I know, std::strinig does not. I'm wondering why you care so much? Do you experience any problems because of that (like memory consumption)?
You can see what toStdString() does here: https://code.woboq.org/qt5/include/qt/QtCore/qstring.h.html#_ZNK7QString11toStdStringB5cxx11Ev
inline std::string QString::toStdString() const { return toUtf8().toStdString(); }
toUtf8() returns a QByteArray and toStdString from QByteArray does:
inline std::string QByteArray::toStdString() const { return std::string(constData(), length()); }
-
@JoeCFD said in effiently convert qstring to std::string or char:
Qt is made for GUI, not for performance
I have to disagree here. Qt is way more than GUI. And Qt contains optimizations like https://doc.qt.io/qt-5/implicit-sharing.html in its containers (and also QString if I'm not mistaken), which C++ std lib does not have.
-
https://stackoverflow.com/questions/14716053/do-stl-containers-use-implicit-sharing
Generally speaking, std C++ is better optimized. I do not use Qt at all if any speed related code is developed.
-
@JoeCFD said in effiently convert qstring to std::string or char:
Generally speaking, std C++ is better optimized. I do not use Qt at all if any speed related code is developed.
I don't know of the current status, especially I don't know about differences between different STL implementations. However, my experience so far is that Qt is better optimized than STL. There is also a reason why EA implemented their own STL derivative. Especially Microsoft's STL used to be slow. I don't know how much that has changed.
Measurements are key for these kinds of statements. My last measurement dates back to 2011. Back then, I used Linux with GCC. I did some profiling of my code and found that most of the time was spend on some simple operations on std::vector. Profiling revealed some unneccesarily deep nesting of function calls within std::vector. I just swapped the variable declaration from std::vector to QVector. This brought a 3x performance gain for my specific problem.
Things might have changed since then (it's been a long time). Nevertheless, I would claim that Qt has different approaches to their implementation of containers. This might give them a performance advantage in certain situations.
Last, but not least, I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.
-
@SimonSchroeder said in effiently convert qstring to std::string or char:
I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.
Not quite. Qt algorithms were deprecated in favour of STL algorithms (e.g.
qSort()
->std::sort()
, see https://doc.qt.io/qt-5/qtalgorithms-obsolete.html ), but Qt containers and STL containers are apples-to-oranges; Qt containers are here to stay.If you compare QVector with std::vector, profiling might reveal a minor performance advantage in one of them. For most applications though, raw performance is not the deciding factor, so this is not always important.
However! If you compare QString with std::string...
QString
is a powerful toolbox with an intuitive API;std::string
is a puny toy box in comparison.