CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Which is best? (by Optimization & Speed)

    Im experimenting something in my codes in MFC...
    i use GetDlgItem function rather than creating a variable for a specific control (to avoid using UpdateData function).

    Which is better among them? GetDlgItem or Creating Control Variable?

    Thanks....

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Which is best? (by Optimization & Speed)

    Quote Originally Posted by DjChris14
    Which is better among them? GetDlgItem or Creating Control Variable?
    It's a tradeoff - using DDX variables is the better design, leads to less code, and it is safer, since you don't have to test whether the control exists and if it is of the right type. OTOH, calling UpdateData is generally slower, since it transfers data for all the controls in the dialog. Besides that, there are situations where you can't use UpdateData() since you don't want to update other controls, so you can't use UpdateData.
    What I don't understand in your question: You are talking about control variables (like CEdit, CListCtrl), not DDX data members (like CString, int) - note that for DDX control members, UpdateData() does nothing, DDX just creates and attaches the control at the beginning. In that case, I don't see any advantage in using GetDlgItem ().

  3. #3
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Arrow Re: Which is best? (by Optimization & Speed)

    i see.. i always find some of the codes from former officemates and sample codes in the net that uses the UpdateData function just to reflect/retrieve the values to the control and vice versa. im pertaining to the DDX variables.. when i try to use the GetDlgItem function in passing or retrieving of values, i just found out that i dont have to use UpdateData function....

    sample :
    void CCompute::Calculate()
    {
    float fResult = 0;
    UpdateData(1); // to get value
    fResult = m_vdPenaltyRate * float(m_viDays); // m_vdPenaltyRate and m_viDays are DDX variables
    m_vsPenalty.Format("%f",fResult); // m_vsPenalty is a DDX variable also
    UpdateData(0); // to Show the result
    }

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Which is best? (by Optimization & Speed)

    Quote Originally Posted by DjChris14
    im pertaining to the DDX variables.. when i try to use the GetDlgItem function in passing or retrieving of values, i just found out that i dont have to use UpdateData function....
    OK, so (according to the code you posted) you are not referring to control variables, but DDX value members. Then note that for the example you posted, DDX does a lot more. To do the same as your code without UpdateData, you would have to:
    • Call GetDlgItem() to get the control as a CWnd*
    • Check whether the control exists and if it is of the correct type
    • Cast it to the correct type (e.g. CEdit)
    • Create a CString variable and call GetWindowText() to retrieve the text (OK, these steps can be simplified if you calleed GetDlgItemText() directly)
    • Check whether the string is a valid numerical string, and display an error message if not
    • Convert the string to a float
    • Optionally, check the range, and display an error message if out of range
    • After the calculation is done, format the result as a string
    • Call GetDlgItem again for the other control
    • Do the checking etc. as described above
    • Set the control's text via SetWindowText.
    The DDX mechanism does all that for you.

  5. #5
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Re: Which is best? (by Optimization & Speed)

    thanks mr.gstercken...

    i just looking about this area due to optimization of codes regards of usage and implimentation in a system.... some of my officemates always use the UpdateData function even thought only one control (DDX variable) was modified... which i found out that it makes the process slower...

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Which is best? (by Optimization & Speed)

    Quote Originally Posted by DjChris14
    thanks mr.gstercken...

    i just looking about this area due to optimization of codes regards of usage and implimentation in a system.... some of my officemates always use the UpdateData function even thought only one control (DDX variable) was modified... which i found out that it makes the process slower...
    Some optimizations aren't worth worrying about. In this case, unless you have hundreds of controls and you're calling UpdateData thousands of times, it isn't worth your time to try to optimize a function that doesn't impede the user or slow execution down noticably anyway.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Which is best? (by Optimization & Speed)

    ...which i found out that it makes the process slower...
    I would be very interesting in seeing a timing study to back this claim up!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Which is best? (by Optimization & Speed)

    I don't know if the speed is so important. Wha I find really annoying when using member variables like that is that all of them get updated. There is no easy way ( if any) to update only 1 member variable.
    Har Har

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured