wxString performance Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

wxString performance

Post by Ronald »

Code: Select all

wxMenuItem* m_menuItem_new;
m_menuItem_new = new wxMenuItem( m_menu_file, wxID_ANY, wxString( wxT("&New") ) , wxEmptyString, wxITEM_NORMAL );
Funciton: wxMenuItem::wxMenuItem
Parameter: name (the 3rd parameter)
Question: Is it not efficient enough to construct a wxString here? Take Qt for example, it provides QStringLiteral to solve the performance problem by eliminate memory allocating when constructing a string object.
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: wxString performance

Post by eranon »

wxT() is not needed anymore since (unless mistake) wxWidgets 3.0. So, you can just create your menu item like this below (and in one line). Here, _() allowing you to translate the label in an associated .mo catalog.

Code: Select all

wxMenuItem* m_menuItem_new = new wxMenuItem(m_menu_file, wxID_ANY, _("&New"), wxEmptyString, wxITEM_NORMAL);
This said, the forum is dedicated to the use of wxWidgets, not its design. If you want to discuss the wxWidgets design, it's better to address your suggestions through mailing list: https://www.wxwidgets.org/support/mailing-lists/.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

eranon wrote:wxT() is not needed anymore since (unless mistake) wxWidgets 3.0. So, you can just create your menu item like this below (and in one line). Here, _() allowing you to translate the label in an associated .mo catalog.

Code: Select all

wxMenuItem* m_menuItem_new = new wxMenuItem(m_menu_file, wxID_ANY, _("&New"), wxEmptyString, wxITEM_NORMAL);
This said, the forum is dedicated to the use of wxWidgets, not its design. If you want to discuss the wxWidgets design, it's better to address your suggestions through mailing list: https://www.wxwidgets.org/support/mailing-lists/.
New to wxWidgets, reading the book "Cross-Platform GUI Programming with wxWidgets" in 2005, which is about 13 years old.
Do you mean _("&New") is more efficient than wxT("&New")?
What does .mo mean?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxString performance

Post by doublemax »

I think you should look for performance issues where they matter. Creating GUI elements is not where performance matters. When you create a new gui element there is so much code executed in the background, the string creation is almost negligible.
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

doublemax wrote:I think you should look for performance issues where they matter. Creating GUI elements is not where performance matters. When you create a new gui element there is so much code executed in the background, the string creation is almost negligible.
wxWidgets is fast enough, however the faster the better with a little modification without any loss of clarity. Take Microsoft MFC for comparison, it uses a pointer for string parameter instead of CString, personally I prefer performance here:

Code: Select all

CWnd::Create

virtual BOOL Create(
    LPCTSTR lpszClassName,  
    LPCTSTR lpszWindowName,  
    DWORD dwStyle,  
    Const RECT& rect,  
    CWnd* pParentWnd,  
    UINT nID,  
    CCreateContext* pContext = NULL);
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxString performance

Post by Manolo »

If your GUI has poor performance, then you GUI is likely not a good design.
If your code has poor performance, better rethink your strategy.

MFC were designed to run only on MSW OS, no Unicode conversions, no OS independant, no so much other features. Perhaps a tiny portion of faster, but lot of lack-of-features code.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxString performance

Post by doublemax »

Take Microsoft MFC for comparison, it uses a pointer for string parameter instead of CString
Nevertheless, the string data will be copied internally.
Use the source, Luke!
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: wxString performance

Post by eranon »

Ronald wrote:Do you mean _("&New") is more efficient than wxT("&New")?
No, I didn't say anything like that (when I want to optimize an app on speed I'm using the compiler's options, then I profile it to decide what are the blocks of code requiring an added effort from me -- and, first, I choose the right language for the right project (wxString handling will be always faster than, say, its Ruby equivalent), I simply suggested to use _() to allow you to translate the word "New" in case your app is a multilingual one. But if your app is monolingual, you don't need to use _() and "&New" will be enough.
Ronald wrote:What does .mo mean?
A ".mo" file contains the translation in one language of all the translatable strings coming from your app (typically, you extract all these strings toward a .po file that you'll edit in a dedicated editor -- e.g. Poedit -- which will be able to compile it in a final compiled .mo format; some hints here: http://docs.wxwidgets.org/3.0/overview_i18n.html)
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

eranon wrote:
Ronald wrote:Do you mean _("&New") is more efficient than wxT("&New")?
No, I didn't say anything like that (when I want to optimize an app on speed I'm using the compiler's options, then I profile it to decide what are the blocks of code requiring an added effort from me -- and, first, I choose the right language for the right project (wxString handling will be always faster than, say, its Ruby equivalent), I simply suggested to use _() to allow you to translate the word "New" in case your app is a multilingual one. But if your app is monolingual, you don't need to use _() and "&New" will be enough.
Ronald wrote:What does .mo mean?
A ".mo" file contains the translation in one language of all the translatable strings coming from your app (typically, you extract all these strings toward a .po file that you'll edit in a dedicated editor -- e.g. Poedit -- which will be able to compile it in a final compiled .mo format; some hints here: http://docs.wxwidgets.org/3.0/overview_i18n.html)
Thanks, helpful.
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

doublemax wrote:
Take Microsoft MFC for comparison, it uses a pointer for string parameter instead of CString
Nevertheless, the string data will be copied internally.
With a pointer, the function gets data from the memory the pointer points to without copy the string, the programmer should guarantee what the pointer points to should be valid.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxString performance

Post by doublemax »

With a pointer, the function gets data from the memory the pointer points to without copy the string, the programmer should guarantee what the pointer points to should be valid.
The called function does not take ownership, which means that the string data has to be copied. Otherwise every string you pass there except literals would have to be allocated on the heap and the caller would be responsible for destroying it (but when?). Which would be very inconvenient.
Use the source, Luke!
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

doublemax wrote:
With a pointer, the function gets data from the memory the pointer points to without copy the string, the programmer should guarantee what the pointer points to should be valid.
It's a const pointer. Which means that a) the called function does not modify it and b) that the called function does not take ownership.

Otherwise every string you pass there except literals would have to be allocated on the heap and the caller would be responsible for destroying it. Which would be very unconvenient.
Yes. Considering multi language, wxString makes more sense. I have spent several days in trying MFC, because 1) it is more efficient for machine 2) the controls can be customized seriously, but found that it was not well supported by M$ now any more. I'll check wxWidgets again. Thanks.
User avatar
shawnhcorey
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Nov 19, 2012 3:29 pm
Location: The Great White North

Re: wxString performance

Post by shawnhcorey »

You should never optimize a program without first profiling it. Without knowing where it is slow, you can spend too much time optimizing the wrong parts.

And you should never optimize a program until users complain it's too slow. Instead, write the program to be easily understood. A professional programmer does not even think about optimization unless they get a change order to speed up a program.
WARNING: Highly caffeinated ☕. Approach with caution 🚧.
Ronald
Super wx Problem Solver
Super wx Problem Solver
Posts: 306
Joined: Mon Mar 05, 2018 4:17 am

Re: wxString performance

Post by Ronald »

shawnhcorey wrote:You should never optimize a program without first profiling it. Without knowing where it is slow, you can spend too much time optimizing the wrong parts.

And you should never optimize a program until users complain it's too slow. Instead, write the program to be easily understood. A professional programmer does not even think about optimization unless they get a change order to speed up a program.
If it can be faster without any loss of clarity of code, why not?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxString performance

Post by ONEEYEMAN »

Yes.
But you missed the first point - did you actually profiled the code?

Maybe the bottleneck is somewhere else?

Thank you.
Post Reply