Announcement

Collapse
No announcement yet.

ForEach Optimization

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ForEach Optimization

    i have this weapon, and it hits 2 things: Pawn's and this other actor i have.

    now im using the VisibleCollidingActors Iterator for the two,
    would it be faster to use 1 that checks for all actors and use casts to check which one is there, or should i use 2 of them (run 1 after the other) for each one?

    #2
    There are two reasonable options: Either two VisibleCollidingActors loops or this CollidingActors loop (not VisibleCollidingActors!)
    Code:
    foreach CollidingActors(class'Actor',thisActor, radius and stuff) {
      if ((Pawn(thisActor) != None || YourOtherClass(thisActor) != None) && FastTrace(thisActor.Location, Location)) {
        // do stuff here
      }
    }
    The reason for using CollidingActors instead of VisibleCollidingActors is outlined on UnrealWiki: Code Optimization/Optimize iterator use.
    It depends on additional checks, which solution is better and I guess you'll have to test it.

    Comment


      #3
      Thank you for your reply, i figured 1 check with an additional if() would be better, and thx for the tip to use colliding not vis, its just vis was the original in it, thx again

      Comment


        #4
        I would guesstimate that foreach(class'Actor') followed by if(myActor is ActorX or ActorY) is slower than a foreach(class'ActorX') followed by foreach(class'ActorY'). In the first case, native code runs through the actor list only once, but your bit of script runs on every type of actor. In the second case, native code runs through the actor list twice, but your script only runs for the correct actor types. Native code is about 20 times as fast as script, so it is usually a good idea to use native code more and script less.

        In this case, you cannot really compare what native code and script are doing... so the only way to get some reliable data would be to run a few little tests. I believe UScript has ways to measure the time between the start and end of the function.

        Comment


          #5
          the thing is though, its only in a small radius around the player

          Comment


            #6
            For smaller radii, (Visible)CollidingActors is faster than RadiusActors. The reason for this is that CollidingActors uses the collision hash (octree and whatnot; actors are grouped by region or something) while RadiusActors iterates over the entire Actors list like AllActors, just with a radius condition.
            The one vs. two iterations question can really only be answered by Clock()ing or StopWatch()ing both versions for similar ingame conditions. Weeding out actors by class natively surely is faster than doing so in UnrealScript, but the real question is: can it make up for iterating over the whole bunch of actors twice?

            Comment

            Working...
            X