Thread: Solutions for slow performance?

  1. #46
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Sebastiani

    BTW, never do:

    delete x;

    Instead,

    if(x) //...or if you like, if(x != NULL)
    delete x;

    This will ensure that you don't crash the proggie from deleting a NULL pointer...
    I like to go one step further. After all of that I set the pointer to null, so if I try and access it later accidently the program will fault. I usually just make a macro for this.
    Code:
    #define SafeDelete( x )    if( x ) delete x, x = NULL;
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  2. #47
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sebastiani,

    You need to use an iterator thingee?? I thought that as you erased an object, the rest of them would move to fill in the gap!

    ...and don't delete the actual vector cells, since the STL will do that for you
    fighters is a vector of pointers; FillYourBrain (in another thread) told me that you had to loop through the vector and delete each one...


    Nick,

    You have to
    be careful with stuft like

    for (int i = 0; i < 40; i++)
    /* do more stuft */

    for ( i = 0; i < 40; i++)
    /* do more stuft */
    All of your classes with virtual functions must have a
    virtual destructor
    Are these related?? (and why do I have to be careful with loops? slow?)

    you're best off generating these ahead of time
    You mean like, copy and paste that code and call it GenerateMask(), then make TransBlt() take a mask and a bitmap?

    P.S. I heard from someone that you should never use i++ in for loops, as it is slower than ++i....
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #48
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Wow Nick!!!! You da man! I tried the GenerateMask() thingee, and when I ran the game with the profiler, TransBlt's time-used vs. running time percentage dropped to around 7%, from a previous 25-27%
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #49
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Good job on the transblit!

    As to your questions, I was assuming the code you posted was attempting to delete the entire vector. Again, in that case, you need to loop through it and delete YOUR objects. Than call a single delete on the vector. What was suspect about your code was that it seemed to delete the first cell only.

    fighters.erase(&fighters[0]);

    instead of

    fighters.erase(&fighters[x]);

    ...if you catch my drift.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #50
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'll try that later with a simpler program (i.e. one that makes a vector of ints, deletes it and quits ).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #51
    Nick
    Guest
    You can delete null pointers. This is valid code
    char* p = 0;
    delete p;
    http://www.parashift.com/c++-faq-lite/

    What I meant was this isn't standard c++ infact in won't compile
    on just about any compiler other than visual c++ 6.0

    for (int i = 0; i < 40; ++i)
    continue;

    for (i = 0; i < 40; ++i)
    continue;

    http://www.mvps.org/vcfaq/lang/1.htm

    Are these related?? (and why do I have to be careful with loops? slow?)
    No let's say you have a class A and B like this

    Code:
    class A 
    {
    public:
          A() { p = new int[40]; }
          ~A() { delete[] p; }
          virtual void draw();
    private:
           int* p;
    };
    
    class B : public A
    {
    public:
            B() { q = new int[40]; }
            ~B() { delete[] q; }
            void draw();
    private:
            int* q;
    };
    
    B* bp = new B;
    A* ap = bp;
    
    ap->draw;
    delete ap;
    Well B's draw function gets called but B's destructor
    does not. Instead only A's destructor get's called. You can
    try this out by writing some simple classes and have
    them printout something in the destructor. This could cause
    a memory leak. Other than that you need to turn up warnings
    on your compile cause most compilers will warn you of this.

    Indexing a vector should be as
    fast an array but erasing from the middle of one is suppose to be slow. http://www.infosys.tuwien.ac.at/Rese...al/prwmain.htm looks like a good tutorial.

    Most people who use visual c++ with the stl use
    www.stlport.com

  7. #52
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Could you start the game in a rocket limit break and profile it then? That's pretty much the only situition where the game runs unacceptably slow.. so that's where you should focus the optimization efforts.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #53
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I could Actually, that never really occured to me, but I pretty much figure it's got something to do with the rockets' movement/drawing code. Lasers, when moving, just move 12 pixels upwards, while Rockets have to calculate the direction to the alien (some trig), figure out which way to turn (some more trig), and then accelerate in that direction (some more trig). Then in the Rockets' drawing code, it has to draw both the rocket and the target

    I'll try the profiler thing thoug...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  2. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 04:18 AM
  3. Observer Pattern and Performance questions
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2007, 11:12 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM