Page 1 of 1
wxString vs wxArrayString vs wxString array
Posted: Sat Jun 20, 2009 9:02 am
I would like to double check which one of these take more memory and can be accessed faster:
1) 100 wxString variables
2) wxArrayString with 100 wxString variables as elements
3) wxString array with 100 wxString variables as elements
Posted: Sat Jun 20, 2009 10:28 am
1. Totally stupid (unreadable code, performance gain ungaranted).
2. More versatile, dynamic, but greedy.
3. Static, not so greedy.
Posted: Sat Jun 20, 2009 10:38 am
I don't mean to seem negative, but you could always test and find out, I would be very surprised if with 100 strings that there was a major difference (apart from the code nightmare that 100 individual variables would bring)! According to the wxArrayString
but the access time to the elements is constant ... it is also very size efficient and doesn't take more space than a C array wxString type
So given its extra versatility it seems like wxArraySting would be the best option.
Posted: Sat Jun 20, 2009 2:33 pm
Best of both Worlds:
std::array<wxString, 100> myArray;
Posted: Sat Jun 20, 2009 11:58 pm
Posted: Sun Jun 21, 2009 8:47 am
I want to let you know that having different names for all strings is not a problem at all in my case. In fact it will be more readable for me since I will load them with certain language file and it will be more easier to know which string to put where.
Therefore, the only thing I'm focused is the memory consumption and access speed. I used number 100 as an example, it may be much more as the code grows.
If having different string rather than array is more faster and less memory greedy I will go this way.
Thank you very much!
Posted: Sun Jun 21, 2009 10:45 am
If performance is really important for you, i would suggest not to use wxString, but rather string, or char*.
A framework is definitely not a good solution for that.
And a char* array is really a better solution than X unconnected char* variables.
I don't know what are your aims, but it must be very very specific. Considering process speed capacity of nowadays processors and other hardware, it's theoritically sufficient enough to use in a real time industrial environment without a RT kernel (i said "theoritically" fast enough, it cannot replace a real RT kernel).
Posted: Sun Jun 21, 2009 11:54 am
the memory usage of all given solutions are pretty much the same as they all use wxString internally, the surrounding container (wxArray, wxArraystring etc.) is pretty much neglectable. Same goes for access speed.
Some more things to consider:
In wx2.8 one wxChar uses 2 (MSW) or 4 (Linux, OSX) bytes.
In wx2.9 wxString uses UTF-8 representation internally, so for ascii-strings this reduces memory usage a lot, but operations that work on individual characters inside the string are slower.
OTOH an empty wxString already uses 40 bytes (MSW, VC7, may be slightly different on other platform/compiler) which can be an issue if you have lots of small strings.
For speed optimization it also depends a lot on what kind of operations you perform on the strings.
Posted: Sun Jun 21, 2009 12:16 pm
Thanks a lot to everyone. It's much more clear to me know.