XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

accesing List<> versus standard array speed difference on X360?

Last post 9/1/2007 3:04 PM by Walaber. 4 replies.
  • 9/1/2007 6:07 AM

    accesing List<> versus standard array speed difference on X360?

    Hello all.  I'm currently working on a soft body physics library for XNA called "JelloPhysics".  the library is to the point that it can be used now, and is available from my homepage.

    Anyway, as usual I got the library working on Windows, and it seemed to run quite well, and then I ported over to 360, and the performance was not exactly stellar.  So I did a lot of research, and thanks to information on the blogs and this forum, I've been able to increase the performance on the 360 quite a bit.  This was mostly through avoiding allocating heap objects in my code, and reducing operator overloading / inlining inner loop math for all of the Vector2's (the engine is all in 2D).

    I have a project I'm working on to test the library, and according to StopWatch, currently my physics.update() function is taking about 0.34ms on Windows, and 1.51ms on X360.  That puts the 360 at about 4.4x slower than the windows code.

    using Nprof I've found where most of the time is spent in the update() function, and I'm trying to optimize those functions as much as I can... however I don't see much left that I can do.


    in my code, I use a lot of List<> containers for keeping track of everything.  I iterate through the List<> with a simple for loop, accessing members with the [] operator.

    I am wondering if anyone has done any profiling to see if accessing members of a List<> is slower than, say accesing an array, on the 360?

    currently I am trying to optimize the following function, and I'm not sure where to go from here.

    The only things I can think is that the = operator on Vector2 might be a function call, so inlining that might give a slight gain, and possibly changing from a List<> to some other container that is faster to access on the 360, if such a thing exists.

    of course, I also probably need to try and come up with an algorithm that doesn't require calling this function as much as I currently am, but the 4.4x difference in speed between my Windows laptop and the 360 seems a bit excessive, so I'm inclined to believe I still have some code in here somewhere that is particularly slow on the 360.

    here is the function.
    public bool contains(ref Vector2 pt)
    {
    // basic idea: draw a line from the point to a point known to be outside the body. count the number of
    // lines in the polygon it intersects. if that number is odd, we are inside. if it's even, we are outside.
    // in this implementation we will always use a line that moves off in the positive X direction from the point
    // to simplify things.
    Vector2 endPt = new Vector2();
    endPt.X = mAABB.Max.X + 0.1f;
    endPt.Y = pt.Y;

    // line we are testing against goes from pt -> endPt.
    bool inside = false;
    Vector2 edgeSt = mPointMasses[0].Position;
    Vector2 edgeEnd = new Vector2();
    int c = mPointMasses.Count;
    for (int i = 0; i < c; i++)
    {
    // the current edge is defined as the line from edgeSt -> edgeEnd.
    if (i < (c - 1))
    edgeEnd = mPointMasses[i + 1].Position;
    else
    edgeEnd = mPointMasses[0].Position;

    // perform check now...
    if (((edgeSt.Y <= pt.Y) && (edgeEnd.Y > pt.Y)) ||
    ((edgeSt.Y > pt.Y) && (edgeEnd.Y <= pt.Y)))
    {
    // this line crosses the test line at some point... does it do so within our test range?
    float slope = (edgeEnd.X - edgeSt.X) /
    (edgeEnd.Y - edgeSt.Y);
    float hitX = edgeSt.X + ((pt.Y - edgeSt.Y) * slope);
    if ((hitX >= pt.X) && (hitX <= endPt.X))
    inside = !inside;
    }
    edgeSt = edgeEnd;
    }
    return inside;
    }



    Go Go Gadget XNA!
  • 9/1/2007 12:24 PM In reply to

    Re: accesing List<> versus standard array speed difference on X360?

    Standard arrays might be faster but I doubt they would be 4 times faster.  One thing that comes to mind is that you have to use multi-threading in the Xbox 360 in order to get full use out of the CPU's.  Shawn Hargreaves talked about this at GameFest.  I believe this is the video.  http://msdn.microsoft.com/xna/videos/gf07_u.wvx
  • 9/1/2007 12:43 PM In reply to

    Re: accesing List<> versus standard array speed difference on X360?

    The List<> class uses an array internally so there is no large difference in performance. There might be a very slight performance cost because of the extra method call but only measuring will tell you if its worth the extra effort to avoid it.

    That function is looking pretty simple, apart from a couple of List<> item lookups it's just a bunch of math operations on floats. It has been said that floating point performance on the Xbox 360 CPU is not as good as your average PC CPU (there are ways of improving things, at the cost of added complexity, on the Xbox 360 CPU but they are not currently available to managed code). You may find that your PC is just plain 4x faster than the Xbox 360 CPU at floating point math. The good news is that the XNA people and Xbox 360 CLR people may be able to improve that so that at some point your code will benefit from a performance improvement at no extra cost to you. Also the Xbox 360 has blindingly fast graphics hardware and a 6 core CPU so there are other options for you to look into.

    Cheers,
    Leaf.

    ps. Love the wobbly physics demo. Very nice.
  • 9/1/2007 1:28 PM In reply to

    Re: accesing List<> versus standard array speed difference on X360?

    I don't see anything particularly terrible about that code: it looks like you've already got all the obvious low-hanging optimisations. Changing the list to an array can probably speed things up, but I would suspect not dramatically.

    But don't trust me, measure for yourself!

    There are fundamentally only four ways to judge performance questions like this:
    1. You might already know the answer based on previous experience with similar problems.
    2. You might be able to infer the answer based on a general understanding of the system in question (maybe you know something about how a function you are calling is implemented, or looked at the code in Reflector to see how it works).
    3. Someone else might tell you the answer.
    4. You can measure the code to find out for sure.

    The problem with relying on anything other than technique #4 is that performance is subtle and unpredictable. Things change. Something that was the bottleneck in one program may turn out to be insignificant in another.

    This makes relying on previous experience or a general understanding very risky, and trusting what someone else tells you even more so. If you ask someone who just spend a week optimizing their code to avoid garbage, they're liable to say "oh yeah, GC is definately the most likely culprit here", but who knows if that applies to your program?

    I'm going to tell you that arrays are faster than lists, by a small but constant linear factor. I have no first hand evidence for this, though. I never timed it myself. I'm basing this partly on things I read on other people's blogs, partly on an understanding of the code (when I was first learning .NET I spent some time looking at the implementation of the various collection classes in Reflector), and partly based on my previous experience with other similar data structures in other languages like C++.

    I might be wrong. My previous C++ experience could be misleading me. The code I saw in Reflector might not have the performance implications I guessed it would, or might not even be the same in the latest version of .NET compared to when I looked at it.

    I might just be an idiot, or be lying to you. Or I might be right, but the difference between a list and an array might be so insignificant that it makes no actual difference to your program at all.

    The only real way to find out is to try it for yourself, and time the difference.
    XNA Framework Developer - blog - homepage
  • 9/1/2007 3:04 PM In reply to

    Re: accesing List<> versus standard array speed difference on X360?

    thank you for the responses everyone.  I'm going to do a bit more profiling, to determine if there are any other areas in the code that are actually causing the slowdown on the 360.  After I squeeze out all of the low-hanging fruit performance gains, I will continue working on my game... possibly eventually making the physics run on a separate thread to see if that helps.

    thanks again, I'll post back more results later.
    Go Go Gadget XNA!
Page 1 of 1 (5 items) Previous Next