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
p.s. excuse my bad writing form
OK first things first, using const declarations rather than #defines
#defines suck for defining numbers n stuff
#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
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
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
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 devicei 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
for (int i = 0; i < BUFFER_SIZE; ++i)
std::cout << array<> << std::endl;
char *start = array;
const char *end = array+BUFFER_SIZE;
while (start != end)
std::cout << start++ << std::endl;
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;
Where a preincrement just does
i += 1;
So again in heavy loops will help shave off a few seconds.
OK i know it was messy but hopefully someone
learnt something, i tend to waffle and forget things so expect more dribble from me soon
Long live c++
Well done MM - excellent post.
You are a real asset here.
LOL, I just read this again, LOL
Long live c++
Yeah dude! Very cool
|Best of The Cache Home|
|Search The Cache|
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Linux Related
- Mac, iPhone & OS-X Stuff
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads