CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    3

    Some performance/optimization questions

    I am in the process of rewriting a simulation application and I need to look at all my options for accelerating the execution. I've never had to concern myself this much with performance So I have a few (3) questions. I have separated the questions below. Thank you in advance for your replies.


    ====QUESTION 1======
    If you completely disregard the benefits of OOD/OOP in terms of maintenance and reusability, can faster performance be achieved through a procedural approach using structs to organize the data? For instance, my simulator needs to do a lot of simple data comparisons so would there be a significant performance difference between the following two approaches:

    Approach 1
    if (record1.testValue > record2.testValue){...}

    Approach 2
    if (obj1.getTestValue() > obj2.getTestValue()){...}

    ====QUESTION 2======
    I almost hate to ask this, but is there any performance boost associated with using globals instead of passing by reference? I know it's ugly to use globals, but I would consider it if there was a return on performance

    ====QUESTION 3======
    which of these would be most efficient under an OOP paradigm? It seems like it should be approach 1, but perhaps I'm wrong.

    Approach 1
    int localTestValue;
    localTestValue = ((obj1.returnNewValue() - 2) % 3)
    if (localTestValue == 2){...}

    Approach 2
    int localTestValue;
    localTestValue = obj1.returnNewValue();
    if ((localTestValue == 2) || (localTestValue == 5) || (localTestValue == 8)){...}

    Approach 3
    if ((obj1.returnNewValue() == 2) || (obj1.returnNewValue() == 5) || (obj1.returnNewValue() == 8)){...}

    Thanks again.

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

    Re: Some performance/optimization questions

    Off the top of my head

    1) 1 would be faster

    2) I doubt it

    3) In most cases 1 would be the fastest and 3 would be the slowest.

    You're not talking about big differences though. You could always try the different scenarios and profile them and see.

  3. #3
    Join Date
    May 2008
    Posts
    3

    Re: Some performance/optimization questions

    Thanks,

    I figured these wouldn't be big differences, but this is going to be a scalability issue for me so I need to do what I can to speed up the app.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Some performance/optimization questions

    It's always best to test optimisation tricks out on some real data and time it. Compilers are pretty smart when it comes to optimisation nowadays and it can be suprising how many 'speed up' tricks make no appreciable difference and only serve to make the code more difficult to read. Named Return Value optimisation is one that comes to mind. This is where, if the compiler detects that the internal value is being returned to a variable, will write the value directly back into that variable avoiding invoking redundent constructor/destructor calls.
    C++ Performance Tips

    One technique often used by real-time developers is to pre-declare and size as many of the containers at initialisation as possible, thereby limiting the amount of heap manipulation during runtime.
    i.e.
    If a class member function uses a tempory vector to store intermediate values then it can be beneficial to make this a class member rather than a function local.
    Last edited by JohnW@Wessex; May 27th, 2008 at 03:53 AM.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Some performance/optimization questions

    Some more here.

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