Re: CString Speed and compiler optimization

From: Richard Formby (rf302_at_cueword.com.au)
Date: 03/13/04


Date: Sat, 13 Mar 2004 09:51:19 GMT


"Marc Lefebvre" <Marc__Lefebvre_pub_NOSPAM@hotmail.com> wrote in message
news:usFgXULCEHA.1128@TK2MSFTNGP11.phx.gbl...
> CString strTemp;
> strTemp = ""; Can you tell me if the compiler optimization
remove
> this affectation ?

No, it would not. [1]

> If I have a member variable in a class (m_strTemp)
> If I put the following line in the constructor
> m_strTemp = "";
>
> Can you tell me if the compiler optimization remove this affectation ?

No, it would not. [1]

> What was the vest way to have an empty string?
>
> strTemp = "";
> or
> strTemp.Empty

Look at the code for CString::operator=(TCHAR *) and CString::Empty(); and
decide for yourself but...

Empty checks for a few things and does a bit of stuff to release, or not,
the underlying storage.

operator= checks (with some function I didn't delve into) if the string
being assigned has zero length. If it does then operator= calls Empty. Hmmm.
Empty is, by definition, quicker.
However, why this interest in what the optimiser does. You yourself can
destroy any benefit the optimiser does by simply, say, initialising a
variable in the wrong place (inside a loop). Also, I have seen people spend
days optimising code by hand that is only called once in an applications
lifetime.

[1] The compiler optimises nearby code, usually at the assembler level. It
has no idea about the other code that may be in a class. In particular it
has no way to know that a CString is initialised, in its constructor, to
empty, so how would it know that assigning an empty string to a freshly
constructed CString is superfluous?

Cheers

Richard.



Relevant Pages

  • Re: CString Speed and compiler optimization
    ... semantics is hidden in the constructor and assignment operators for CString, ... there is no way to tell that the original contents were empty already. ... >Can you tell me if the compiler optimization remove this affectation? ...
    (microsoft.public.vc.mfc)
  • Re: CString not working as advertised
    ... The following will not compile in a UNICODE application: ... CString kindOfFruit; ... Rather, the compiler should first construct a temporary CString using the const char* constructor, and then initialize kindOfFruit using the copy constructor. ... none of the CStringT constructors is explicit. ...
    (microsoft.public.vc.mfc)
  • Re: TREE structure in MFC, how, where and why?
    ... I had not noticed the CString. ... >By using malloc, the constructor on your CString is not being called. ... >>terminal node and a tree node is that a terminal node has no children. ...
    (microsoft.public.vc.mfc)
  • Re: CObject
    ... // the CObject header ... CString address, ... // Default Constructor CLeagueEstab ...
    (microsoft.public.vc.mfc)
  • Re: problem adding variable to class header file
    ... Like I said, I was adding it in the header file or more specifically, in the dialog header file. ... It wouldn't help you much to add a variable in the constructor since you wouldn't be able to use it elsewhere. ... to add a variable to the class, the program crashes. ... If I declare the variable in say the constructor, it works fine and it does not matter what type of variable whether a CString, int, or whatever. ...
    (microsoft.public.vc.ide_general)