The Cache: Technology Expert's Forum
Welcome, Guest. Please login or register. September 17, 2019, 05:51:13 PM

Login with username, password and session length

Pages: [1]
Author Topic: General nice things and optimization  (Read 3060 times)
Offline Offline

Posts: 240

Check it!

View Profile
« on: May 13, 2007, 10:11:19 AM »

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 Tongue

p.s. excuse my bad writing form Tongue

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
instead of
int GetXCoordinate();
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 Tongue
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[X] 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;

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 learnt something, i tend to waffle and forget things so expect more dribble from me soon Cheesy

Long live c++ Smiley

I am Tyler Durden
Olde World Hacker
Offline Offline

Posts: 10096

View Profile
« Reply #1 on: May 13, 2007, 08:22:37 PM »

Well done MM - excellent post.


It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Offline Offline

Posts: 585

View Profile
« Reply #2 on: May 14, 2007, 03:50:27 AM »

Thanks dude,

You are a real asset here.


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

Long live c++

Yeah dude! Very cool  Praise

« Last Edit: May 14, 2007, 03:56:55 AM by thedarkness » Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Offline Offline

Posts: 240

Check it!

View Profile
« Reply #3 on: May 14, 2007, 05:37:21 AM »

Haha thanks Cheesy
Woop! \o/

I am Tyler Durden
Pages: [1]
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks

Valid XHTML 1.0! Valid CSS!