You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, August 1st 2010, 9:14pm

QString operations without deep copies

Excuse me if this is painfully obvious ...

Can you perform QString operations without deep copies. I'd like to trim and make the string upper case in place (no deep copies).

Thanks

2

Sunday, August 1st 2010, 9:39pm

you could use the [] Operator and perform trimming and toUpper on your own, char by char.

3

Sunday, August 1st 2010, 9:48pm

I was hoping the functionality was there somewhere. The [] operator drops you to a QChar, not a char, doesn't it?

To avoid the overhead of memory allocation, etc., I assume you'd need to get to the raw data, but I was hoping to avoid that just in case the raw data character changes in the future.

4

Sunday, August 1st 2010, 10:10pm

it drops you to QCharRef. There you will have functions like toUpper
available. So you can do:

Source code

1
myQString[i] = myQString[i].toUpper().


What do you mean with raw data :huh: ?
Do you mean the format QString uses internally to represent the string?
Access to that would be available only by subclassing QString.
With the solution I described you should not have to get that raw data, cause
there should not happen any allocation to get the QCharRef and
assigning back to it ;) .

Apart from that, what are you trying to do :huh: ?
Trimming and upper-casing, ok, but why is efficiency so critical to you
that simple string copy is not affordable?

This post has been edited 6 times, last edit by "thrall" (Aug 1st 2010, 10:29pm)


5

Sunday, August 1st 2010, 11:50pm

I'm new to Qt, so I don't really know how fast/efficient various pieces are. The function will potentially perform the operations millions of times each operation cycle with parsing, comparing and manipulating the string data only the first part.

So I just thought a way for the QString to manipulate its internal structure without reallocating each time would help on efficiency. And if I need to do my own manipulations at each step, then I may need my own class. But I was hoping to avoid it.

6

Monday, August 2nd 2010, 5:32am

Do you have such a huge base of strings that you have millions of trim and uppercasing operations? And what are cycels in your application?



I dont know your task at hand, but maybe you use wrong approach?

If I may guess from what you've said I assume you have a couple of strings and you try to compare those with input from user in a loop? If so try to convert those strings before you enter loop, and macke shure each string is converted only once.



You could also try to compare using hasing values, those are much faster. Make your hash funciton case insensitive (I guess this is is the reason for upcase). If you have an potential hit make your complete string comparison to check it the hit is true. This reduces comparison operations.



Or, if my assumptions are all wrong, you should check if your algorithm might be the wrong.



Hope this helps.

yasin_motcu

Beginner

  • "yasin_motcu" is male

Posts: 32

Location: Türkiye

Occupation: Computer Engineer

  • Send private message

7

Monday, August 2nd 2010, 8:09am

Qstring dummy_str;

dummy_str.trimmed().toUpper();
Don´t let your fears stand in the way of your dreams!

8

Monday, August 2nd 2010, 1:01pm

Unless I'm misunderstanding the documentation, wouldn't

myQString.trimmed().toUpper();

actually produce 2 allocations?

In response to Thrall, my question is really more just trying to understand the tradeoffs in adopting Qt in different areas. If I need a lot of very fast string operations (and in my context I do), it would seem that having a QString be able to operate on the data it already holds would be a lot faster than allocating another QString everytime a simple operation took place. The algorithm isn't the issue (no multiple compares in a loop, etc.).

But I'm new to Qt and don't really know speed/implementation details thus my question.

yasin_motcu

Beginner

  • "yasin_motcu" is male

Posts: 32

Location: Türkiye

Occupation: Computer Engineer

  • Send private message

9

Monday, August 2nd 2010, 1:24pm

I think the following link will solve your problem.

http://doc.qt.nokia.com/4.6/qstring.html#fromRawData
Don´t let your fears stand in the way of your dreams!

10

Monday, August 2nd 2010, 6:40pm

Unless I'm misunderstanding the documentation, wouldn't

myQString.trimmed().toUpper();

actually produce 2 allocations?

QString:toUpper() will copy, trimmed() is not documented enough to do so, but taking a lock into source code src\corelib\tools\qstring.cpp reveals, that trimmed() returns newly created QString initialized with trimmed data:

Source code

1
return QString(s + start, l);


which calls QString::QString(const QChar *unicode, int size). There an allocation an copy operation is performed.


Some expert may correct me, but I believe both toUpper() and trimmed() will allocate and copy.

This post has been edited 2 times, last edit by "thrall" (Aug 3rd 2010, 5:27am)