program performance

Started by
7 comments, last by bluepig.man 11 years, 4 months ago
how can l got a balance between performance and code standard.
for example, a varible shoule be in the class.But for the performance, it should be a global varible.
What should l do?
Advertisement
Your example doesn't make sense. "Being in a class" and "being global" is not mutually exclusive and neither of these are directly connected to performance.

So, please clarify where the conflict is and why might be able to help!
Always go for clear and readable code first, and only do micro-optimizations when you know for certain you need it, preferibly when your profiler tells you that it is indeed a performance bottleneck.
Honestly in my opinion you should never sacrifice performance for conforming to some standard. Try everything. If you find that you gain a bunch of cycles in a critical piece of code by messing with how your variables are accessed, then by all means, that is the correct thing you should be doing, regardless of what standard you are now failing to conform to.

It really depends on who's going to end up reading your code or who will be using it, though. If you're virtually the only one that's going to be looking at your renderer code, for example, then by all means document what you're doing and mangle the code into oblivion if it performs fast as hell.

As PAndersson stated, though, it shouldn't be the first thing you aim for. You should start with something simple that you know will get you the correct results first, and then worry about turning your code into a nebulous mindsink later.

What should l do?
[/quote]
Does your game play fast enough on your target hardware?

  • If so, stop making purely performance oriented changes.
  • If not:

    • Profile your game and discover the biggest bottleneck.
    • Next, apply any algorithmic optimisations that are appropriate.
    • Next, look at the memory access patterns and see if they can be made cache friendly.
    • Finally, resort to lower level tricks as a last resort.
    • Rinse and repeat.



That construct A might be faster or slower than construct B is not always important. For example, crippling your project's input architecture to save a handful of cycles per frame is not usually a smart engineering move.

The question then becomes, what analysis have you done to decide that a global is faster than a class member? Is it a general rule, or something specific to this case? Can it scale? Does the change have any impact elsewhere?

Your example doesn't make sense. "Being in a class" and "being global" is not mutually exclusive and neither of these are directly connected to performance.
So, please clarify where the conflict is and why might be able to help!


l'm sorry,the description about problem is blurry.
the code is like this:
[source lang="cpp"]extern vector<string> pacman_map;

class pacman_game
{
pacman hero;
public:
pacman_game(const int &px,const int &py);
};

class pacman
{
friend pacman_game;
move();
public:
pacman();
};[/source]
pacman::move and pacman_game will use pacman_map,so l put it on global scope. But l am worry about that problem.
from grammatical speaking,the map should be put in the class,it's a part of the pacman_game.(the pacman_game is designed as game system class)
However,for convenience and performance(if the map is put in the pacman_game,l should pass the map into hero member),put the map on global scope may the common way.
This should not be your bottleneck. If it is, I imagine you haven't implemented some of the more interesting bits, such as collision detection, path finding and ghost AI.

As for the design, I would recommend not passing the map into the hero, but instead let the game determine if a collision has occurred by asking the pacman for its bounding shape, and if necessary calling a function informing the pacman of the collision.

Something like this:

class Pacman
{
public:
Rectangle bounds() const;
void onHitWall();
void onHitPellet();
void onHitFruit();
void onHitGhost();
// ...
};

class Ghost
{
public:
Rectangle bounds() const;
void onHitPacman();
// ...
};

class Game
{
public:
void update();
// ...
private:
Pacman pacman;
std::vector<Ghost> ghosts;
Map map;
// ...
};

void Game::update()
{
// ...

// During movement
if(map.collision(pacman.bounds()))
{
pacman.onHitWall();
// resolve collision
}

// ...
Rectangle pacmanBounds = pacman.bounds();
for(Ghost &ghost : ghosts)
{
if(pacmanBounds.intersects(ghost.bounds()))
{
pacman.onHitGhost();
ghost.onHitPacman();
// ...
}
}

// ...
}

This is just a quick example. I've never implemented pacman, it might be better to do a pre-emptive check to see how far pacman can move, rather than detect after the fact that he should not have moved so far.

Globals as a "convenience" is questionable, in my experience it doesn't scale well. In addition, any global makes it hard to unit test.

Globals as a "convenience" is questionable, in my experience it doesn't scale well. In addition, any global makes it hard to unit test.


Globals are also more likely to be a performance problem as chances are their content is NOT going to be in the CPU's cache and given that memory access is VERY slow when compared to instruction execution speed you are looking at a stall right there.
In fact,the game is semis.it's use windows api. As soon as this version is finished.the next version(use DirectX) will be started.
This version is used to test the game logic. The AI and path search is a bit complicated,need some time to learn it.biggrin.png
At beginning of this game,Estimated need to spend two weeks.But now,l think it will spend a lot of time.But the process is full of interesting.
And l'm really happy to your answer.

This topic is closed to new replies.

Advertisement