The right way to code.

This is a discussion on The right way to code. within the C++ Programming forums, part of the General Programming Boards category; My application now becomes an oversized giant. There's a small lag and performance drop as the code keeps getting bigger. ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    The right way to code.

    My application now becomes an oversized giant. There's a small lag and performance drop as the code keeps getting bigger. I suspected that it's all because of the unorganized and poor OOP implementation in my part. Here's what I did (and didn't) do. Oh and FYI, I'm making a simple game using SDL:

    1. I load all the bitmaps into SDL_surfaces in the memory (200 megs of images BTW). And from those surfaces, I created objects like background, sprites, etc.

    2. Didn't do any design pattern whatsoever. I created classes when I think I need one.

    3. No inlined, or static function / variable

    4. Few assert and exception handler

    5. Most of the variables are referenced by pointer (eg. surfaces, sprites, etc)

    6. My animation routine is like this:
    1. Blit the whole background
    2. Blit objects
    3. Flip the surface
    4. Loop to 1

    The code itself is one big mess. My questions are:

    1. Is the performance boost from an inlined function significant to the whole application's performance?

    2. If I want to create objects / variables that can be accessed by multiple objects and if the accessed objects / variables were changed, the change will be carried out to all the accessing objects, is it better to use static or a reference by pointer ? What's the benefit of static over a reference by pointer (or vice versa)?

    3. Do unused functions and variables in a class have any impact to the performance?

    4. Does any of you have any advice to help me make a better code?

    Thanks alot in advance.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Search C++ inline functions you will find a lot of stuff... most of it will say profile your code with and without to tell if inlines are helping.

    Being static and being referenced by a pointer are two different things, express this in code example please.

    Yes, because space has to be alocated for all parts of an object.

    Yeah, rewrite it from scratch . Seriously though, first find parts that are obviously lagging down your program, and rewrite those, then when you can't tell as easily look into profiling your code to see where the bottlenecks are and work on fixing those up. This is where design patterns are something you need. That way you make interfaces to areas of your code, and can change the implementation without worrying about the affects on the rest of your code.

    You will iterate through this "find the slow stuff or stuff that doesn't run how you want to to"-> "rewrite it." thing untill the program as a whole satisfies you. Then you will release it (probably) and users will do the first part (aka bugs) and leave the second part up to you.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It is extremely hard and generally a bad idea to guess at performance bottlenecks in code you see. It's impossible to do it with code you only get to describe. As Wraithan said, profile the code.

    But if I had to guess, I'd say the performance penalties come from the 200MB of pictures you have loaded (how much RAM do you have?), especially as you can't keep them all in graphics memory all the time and it will probably be used poorly.

    More bad performance might come from a poor choice of algorithms on you part. Not knowing what kind of game you're making, I can't even begin to guess what algorithms you might have, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Wraithan
    Search C++ inline functions you will find a lot of stuff... most of it will say profile your code with and without to tell if inlines are helping.

    Being static and being referenced by a pointer are two different things, express this in code example please.

    Yes, because space has to be alocated for all parts of an object.

    Yeah, rewrite it from scratch . Seriously though, first find parts that are obviously lagging down your program, and rewrite those, then when you can't tell as easily look into profiling your code to see where the bottlenecks are and work on fixing those up. This is where design patterns are something you need. That way you make interfaces to areas of your code, and can change the implementation without worrying about the affects on the rest of your code.

    You will iterate through this "find the slow stuff or stuff that doesn't run how you want to to"-> "rewrite it." thing untill the program as a whole satisfies you. Then you will release it (probably) and users will do the first part (aka bugs) and leave the second part up to you.
    I've searched the web about inlining, and it's pretty much like you said.

    About static and a reference by pointer example:
    Pointer:
    Say, if I have two objects instances from a this class A:
    clas A
    {
    public:
    B *pointerOfB;
    void createB() {pointerOfB=new(B)}
    operator=(A*) { pointerOfB=A->pointerOfB }
    }

    named A1 and A2. In A1, I new-ed B by calling createB(). And in A2, I called the copy constructor A2=A1. CMIIW, the pointer reference of B in A1 and A2 will be the same. Would it be better if I use the pointer reference or just make the B variable static?


    Quote Originally Posted by CornedBee
    It is extremely hard and generally a bad idea to guess at performance bottlenecks in code you see. It's impossible to do it with code you only get to describe. As Wraithan said, profile the code.

    But if I had to guess, I'd say the performance penalties come from the 200MB of pictures you have loaded (how much RAM do you have?), especially as you can't keep them all in graphics memory all the time and it will probably be used poorly.

    More bad performance might come from a poor choice of algorithms on you part. Not knowing what kind of game you're making, I can't even begin to guess what algorithms you might have, though.
    Yeah, that's what I thought too. But I think it's better to load all the bitmaps than only load 'em when needed from the file. I mean there's the file reading cost and all. I think it's will be slower if I keep reading bitmap files, creating and freeing surfaces all the time. BTW, the reason it's so big is because it's in 32 bpp and in a resolution of 1024X768 (the background). And my RAM is 512 megs.

    Well, the only algorithm I use is the blitting algorithm because it's not the sophisticated type of game. Only a board game.

    Oh yeah another question please, I know that this question seemed rather elementary, but how do I profile my code?

    Thanks.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    > Oh yeah another question please, I know that this question seemed rather elementary, but how do I profile my code?

    If someone tells you to profile your code, it usually means that person wants you to install and run a tool called a profiler, such as this one or this one. In addition, some IDEs come with profilers already built in. People have their favorites I suppose, but there are many to choose from, and you'll want to get one that works with your compiler.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by citizen
    > Oh yeah another question please, I know that this question seemed rather elementary, but how do I profile my code?

    If someone tells you to profile your code, it usually means that person wants you to install and run a tool called a profiler, such as this one or this one. In addition, some IDEs come with profilers already built in. People have their favorites I suppose, but there are many to choose from, and you'll want to get one that works with your compiler.
    OIC, thanks. BTW, is there any free and integratable to KDevelop C++ profiler out there? Dunno if I have any money to buy the commercial one.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    KCachegrind. In fact it is right there under "Debug". (NB. I have no experience with it.)

    Or just compile with -pg and use gprof(1).
    Last edited by zx-1; 10-02-2006 at 04:51 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Ah, thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21