Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString::compare() vs converting QString to numbers and then comparing

Tags:

c++

qt

Is it faster to compare 2 QStrings containing numbers, or to convert those QStrings to numbers and then compare the numbers?

so which is faster?

QString str1,str2;
if(str1.compare(str2)==0)

OR

QString str1,str2;
if(QString::number(str1)==QString::number(str2))

The reason I'm asking is because I have to fill a QMap with error codes and error messages corresponding to those error codes. I'll be reading the error code / error message from an ini file, so I'm wondering whether it's better to convert the error codes to integers and have QMap<int,QString> or to just keep them as QStrings and have QMap<QString,QString>. Which approach will give me the most optimal code?

Where the QMap contains <error code, error message>

like image 777
PTBG Avatar asked Mar 25 '26 04:03

PTBG


1 Answers

String comparison is likely to end with trouble: "1.00" != "1.0" != "1" != "0001"

Always use numeric types for comparing numbers, and don't worry about imagined performance issues of such a minuscule piece of any whole.

like image 196
Gordon Avatar answered Mar 27 '26 20:03

Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!