6 Tips For Efficient Netbook C++ Code

Printer-friendly version

So you want to develop a lean and responsive application for the Netbook?

If you are like most developers, form and function come first followed by application performance (Alright, some of us give consideration along the way as well). Considering that as ATOM developers we are working with an amazing platform, we must also recognize that we are working with a finite amount of hardware resources. The less CPU cycles we utilize to perform a particular function, the better the user experience will be in terms of speed and reaction time.

Keep the following tips in mind when developing your next Netbook entry and realize a leaner and more efficient solution:

1.) Initialization is less expensive then declaration + assignment.

Initialize your variable when they are declared rather than declaring, then assigning later.
* As an added benefit, this helps delay declarations until they are truly required.

Example: MyClass _TheClass = new MyClass(param1, param2);

2.) Do not declare variables until you absolutely need them

This allows us to minimize the allocated memory until we actually need it. Rather than declaring 20 variables at the beginning of a method, declare as you go, allowing for proper scoping as well.
* Tip 1 assists us in this practice as well

Example:

MyClass _TheClass = new MyClass(param1, param2);

if(_Var1 == 1)
{
// Imagine a long loop here...
// _AnotherClass will not sit in memory until we are ready to do something with it
}

MyClass _AnotherClass = new MyClass(param1, param2);

3.) Pass parameters by reference rather than value

By passing by reference in stead of by value we will eliminate the copy constructor and save resources.
Note: Passing strings by reference can be 20-30 times faster!

Example:

void AddNumbers(const int& x, const int& y) { ... }

4.) Pre increment or pre decrement in long loops

If you must use lengthy loops (and who doesn't from time to time?), pre increment or pre decrement your integers. Compared to post increment or post decrement you will cut your CPU cycles in half.

Example:

for ( i=0; i<100; ++i)

5.) Keep "new" to a minimum

When possible use automatic variables, which will utilize the stack. This will both performance as well as code management.

6.) Don't forget about perceived performance

This is the only subjective tip in tis list, although it is quite important. Making the user interface "feel" fast is just as important. This may mean handling UI updates first, then begin your CPU intensive process. It may mean checking your Windows Message Pump more often, or even adding an interesting progress indicator. The key is to give the perception of a smooth and efficient execution. Setting the users expectations in the applications documentation is another way to boost this aspect of performance.

These are only a few tips (although important) to help ensure your Netbook application runs a little quicker. Best of luck!

4.8
Average: 4.8 (5 votes)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Posted On : January 13, 2010 - 16:38
JamesRM (not verified)

I understand the other cases (although not very well number 5) but why is that of number 4?

Thank you very much.

Posted On : January 14, 2010 - 04:24
danio (not verified)

"If you must use lengthy loops (and who doesn't from time to time?), pre increment or pre decrement your integers. Compared to post increment or post decrement"

For most compilers this is not true - pre-increment and post-increment of integers will be optimised to the same machine code.
For classes (such as iterators) pre-increment will generally be faster and should be preferred unless the original value is required.
The C++ FAQ discusses this in detail in "Which is more efficient: i++ or ++i?" at http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15

However there's nothing wrong with getting in the habit of using pre-increment for integers too: just don't expect to cut your CPU cycles in half!

Posted On : January 14, 2010 - 04:43
BrianDevArch's picture
Offline
Last seen: 2 weeks 3 days ago
 Black Belt (Community Leadership) Red Belt
Joined: Nov 13 2009
Points: 62276

James,

In reference to number 4:

* Postincrement will create a copy then increment, then return the copy
* Preincrement will increment then return the original

Posted On : January 14, 2010 - 04:54
BrianDevArch's picture
Offline
Last seen: 2 weeks 3 days ago
 Black Belt (Community Leadership) Red Belt
Joined: Nov 13 2009
Points: 62276

danio,

This is compiler specific, most compilers will create fewer ops for pre notation. Depending on how your code is optimized by the compiler, even those compilers that are smart enough to change post's to pre's may miss it.

When dealing with non primitive types: ++iter will always be more efficient.

The reason ++iter is more efficient comes down to the work that each needs to do to obtain their return value. Whether the return value is actually used or not doesn't matter.

iter++ returns a temporary copy of the variable (create q temp variable with the old value, increment, then return temp var)

Even a great optimizer will not compensate if you do not understand the cost of certain operators. There are many references on the internet and in print and the consensus is that pre incrementing will never be slower than post incrementing, and in most cases pre incrementing is faster due to less operations.

Posted On : January 15, 2010 - 12:24
Offline
Last seen: 2 weeks 3 days ago
Joined: Jan 11 2010
Points: 5

JamesRM, post-incrementing returns a copy of the variable and then increments the variable. Pre-incrementing simply increments the variable and returns it. The penalty is in the creation of the intermediate variable. If the incremented variable is more complex, the penalty is even heftier.

Posted On : January 20, 2010 - 02:29
Bartlomiej (not verified)

Thanks for this interesting post. I have to admit I wasn't aware of some problems you described and e. g. used always the post-incerementation (just because of the habit) and tried to declare all variables at the beginning of the block (readability and backward-almost-compatibility with plain C).
Do the rules apply to other platform also (only they are less important) or they are specific to Netbooks/Atom?

Posted On : January 20, 2010 - 04:42
BrianDevArch's picture
Offline
Last seen: 2 weeks 3 days ago
 Black Belt (Community Leadership) Red Belt
Joined: Nov 13 2009
Points: 62276

Bartlomiej,

These tips do apply to any flavor of C++, and some other languages. When the ATOM SDK becomes available for.NET I have many more to share. These performance tips become more critical on mobile platforms due to the reduced resources. Thank you for the feedback.

Posted On : April 18, 2010 - 03:32
Anonymus (not verified)

Tip 4 seems to be very compiler specific:
The gcc compiler generates the same code for both:

for ( i=0; i<100; ++i)
for ( i=0; i<100; i++)

Posted On : July 12, 2010 - 09:12
Marcos (not verified)

Tip 4 is generally ok, but the actual example given is not:

"void AddNumbers(const int& x, const int& y) { ... }"

Passing primitive types by reference results in less efficient code than passing them by value. A reference is just a pointer, so the code above will require an unnecessary level of indirection just to access x and y. I would pass a primitive type by reference (though not "const") only if I expected the actual variables to be modified by the called function.

Posted On : August 2, 2010 - 11:07
RSanders (not verified)

Thanks a lot. The tips are great. They are short but very clear and important. As for me, I'm not pro in it yet, that is why such tips are of great value to me. I learn developing on my own but it is not as difficult nowadays as it can seem. You can find everything in the internet. To heve books I use pdf SE http://www.pdfok.com , to have advice I use such posts as you. Keep on going with your work. I'm sure I'm not the only one whom these posts helped.

Posted On : August 9, 2010 - 02:52
Medyum (not verified)

)

Tip 4 seems to be very compiler specific:
The gcc compiler generates the same code for both:

for ( i=0; i<100; ++i)
for ( i=0; i<100; i++)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.