You Are Here:

Community: Wiki

This page was last modified on 19 November 2007, at 20:09.

How to optimise code

From Forum Nokia Wiki

Code Optimisation

The following optimisations can be easily added to existing code.

1 Initialisation lists:

CFasterClass::CFasterClass( const TDesC& aInfo ) : iInfo( aInfo )

   {
   }

is better than:

CSlowerClass::CSlowerClass( const TDesC& &aInfo )

   {
        iInfo = aInfo;
   }

The reason why the former is quicker is because only the copy constructor will get called. In the latter example the assignment operator is called too along with the variable’s default constructor.

2 Optimise For Loops

It is always quicker to count down to zero. Therefore, it is quicker to write; for (i = n-1; i >= 0; --i) as opposed to, for (i = 0; i < n; ++i)

3 Initialise on Declaration

Try to initialise variables straight away,

   TMyClass x = data; 

is faster than

   TMyClass x;
   x = data;

Declaration then initialization invokes the object's default constructor then its assignment operator. Initializing in the declaration invokes only its copy constructor.

4 Switch Cases

Always put the most commonly used cases first. Obviously less code will be executed this way.

5 Delay Variable Declarations

Only declare variables when they are needed. Not only is spaced saved but its constructor will be called, which can be a waste of CPU if that variable will not be used. If you are unsure if a costly object will be used at all, consider using “lazy initialisation”

6 Pass By Reference

Always try to pass classes by reference rather than by value. For example, use

   void foo( CFastClass& aObject )

is more optimised than

 void foo( CSlowClass aObject )

7 Use 'op=' Operators

It is generally better to use the ‘op=’ instead of ‘op’

   x += value;

is more optimised than,

   x = x + value; 

That way no temp object is created. The downside of this is that it can make the code harder to read.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ