program performance
#4 Members - Reputation: 160
Posted 16 December 2012 - 05:11 AM
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.
Edited by darkhaven3, 16 December 2012 - 05:13 AM.
#5 Moderators - Reputation: 5304
Posted 16 December 2012 - 07:10 AM
Does your game play fast enough on your target hardware?What should l do?
- 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?
Edited by rip-off, 16 December 2012 - 07:16 AM.
#6 Members - Reputation: 289
Posted 16 December 2012 - 08:49 AM
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.
Edited by bluepig.man, 16 December 2012 - 09:54 AM.
#7 Moderators - Reputation: 5304
Posted 16 December 2012 - 11:15 AM
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.
#8 Moderators - Reputation: 4115
Posted 16 December 2012 - 11:21 AM
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.
#9 Members - Reputation: 289
Posted 16 December 2012 - 05:58 PM
This version is used to test the game logic. The AI and path search is a bit complicated,need some time to learn it.
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.






