m0nkeymafia

Ill group all these together, as always you either know it or u dont, but can improve the speed of your c++ apps, make them more robust, AND give you added type safety - wow coding has never been so sekzy Applause

p.s. excuse my bad writing form Applause

OK first things first, using const declarations rather than #defines
#defines suck for defining numbers n stuff
i.e.

#define MY_BUF 16*1024; //16k buffer
const int MY_BUF = 16*1024;

if you use the const int version, then you get type safety when the compiler does its thing.
Also it means you can put them into different namespaces keeping your system clear of random declarations u dont need everywhere

<>using const elsewhere
const can be used everywhere and to great effect, say you have an accesor function
instead of
int GetXCoordinate();
use
int GetXCoordinate() const;

This basically tells the compiler nothing is altered within that function both speeding it up and reducing the memory footprint of your application.  it also reinforces good coding as the function TELLS you what ti does.

you can also use const to protect returned objects
i.e.

MyObject *GetMyObject() const;
would normally return a pointer to the object but it could be changed.
const MyObject *GetMyObject() const;
this now means the returned object cannot be modified, again makes things safer and better in every way Applause
again this can be used like so:

void SetMyObject(const MyObject *pObj);
this basically means when you pass the object to the function it will not be changed.

All in all doing this will not only speed up your app, make it smaller, but will also help you remember what a function does 6 months later and give you more trust in its operation.

<>Pointer arithmetic and increments
OK in my last post about

duffs device

  i used some crazy pointer notation, so here is a few tricks you can use to speed things up

when you reference a buffer like this: array<> the compiler actually does this:
array + X; which is the memory location of array + X * [size of individual element in array] to get to the location in memory

So if you loop round a 32k buffer you have an incredibly high number of extra lookups.
if your compiler is good it will automatically do this, but with complicated code you have to do it yourself do this instead

Original:
for (int i = 0; i < BUFFER_SIZE; ++i)
{
std::cout << array<> << std::endl;
}

New version:
char *start = array;
const char *end = array+BUFFER_SIZE;
while (start != end)
{
std::cout << start++ << std::endl;
}

Much faster
Note also how in the first loop i use ++i rather than i++
This is because a post increment actually involves a copy as it does something like this:

int old = i;
i += 1;
return old;

Where a preincrement just does
i += 1;
return i;

So again in heavy loops will help shave off a few seconds.

OK i know it was messy but hopefully someone

learn

 t something, i tend to waffle and forget things so expect more dribble from me soon Applause

Long live c++ Applause

perkiset

Well done MM - excellent post.

/p

thedarkness

Thanks dude,

You are a real asset here.

Cheers,
td

[edit]LOL, I just read this again, LOL

Long live c++


Yeah dude! Very cool  Applause

[/edit]

m0nkeymafia

Haha thanks Applause
Woop! o/


Perkiset's Place Home   Politics @ Perkiset's