Click to See Complete Forum and Search --> : Some performance/optimization questions


cassius
May 26th, 2008, 06:53 PM
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.

GCDEF
May 26th, 2008, 07:26 PM
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.

cassius
May 26th, 2008, 07:56 PM
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.

JohnW@Wessex
May 27th, 2008, 03:45 AM
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 (http://www.cs.cmu.edu/%7Egilpin/c++/performance.html)

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.

JohnW@Wessex
May 27th, 2008, 03:49 AM
Some more here (http://www.devx.com/cplus/Article/16328/0/page/1).