Jump to content

  • Log In with Google      Sign In   
  • Create Account

program performance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 bluepig.man   Members   -  Reputation: 412

Like
0Likes
Like

Posted 16 December 2012 - 04:53 AM

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?

Sponsor:

#2 Rattenhirn   Crossbones+   -  Reputation: 1752

Like
0Likes
Like

Posted 16 December 2012 - 05:01 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!


#3 PAndersson   Members   -  Reputation: 394

Like
1Likes
Like

Posted 16 December 2012 - 05:09 AM

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.

#4 darkhaven3   Members   -  Reputation: 160

Like
-1Likes
Like

Posted 16 December 2012 - 05:11 AM

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.

Edited by darkhaven3, 16 December 2012 - 05:13 AM.


#5 rip-off   Moderators   -  Reputation: 8222

Like
2Likes
Like

Posted 16 December 2012 - 07:10 AM

What should l do?

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?

Edited by rip-off, 16 December 2012 - 07:16 AM.


#6 bluepig.man   Members   -  Reputation: 412

Like
0Likes
Like

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 rip-off   Moderators   -  Reputation: 8222

Like
1Likes
Like

Posted 16 December 2012 - 11:15 AM

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.

#8 phantom   Moderators   -  Reputation: 7278

Like
0Likes
Like

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 bluepig.man   Members   -  Reputation: 412

Like
0Likes
Like

Posted 16 December 2012 - 05:58 PM

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.Posted Image
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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS