Forum Replies Created
-
AuthorPosts
-
Damn you’re right parameters are much faster than structs! A lot faster!
I even made my own test to check the speed difference for a similar implementation to the one in my game, and primitives values are significantly faster. As I understand it this has to do with the time it takes to copy structs from one method stack to the next.
I also found that using Getters to get the primitives also significantly affects the time it takes. I assume this is also to do with copying the (“pass-by-value”) value from the one method stack to the next as opposed to just reading it directly from the class’ memory in the heap.
I may have to consider changing my game to just passing the primitives separately (instead of combining them inside a struct) since the performance impact is so much more than I anticipated.
Thanks for the heads up!
<!–more–>
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114#Import "<std>"Using std..' Calculate distance between two coordinatesFunction CalculateDistance:Float(pos1:Vec2f, pos2:Vec2f)Return CalculateDistance( pos1.X, pos1.Y, pos2.X, pos2.Y )EndFunction CalculateDistance:Float(x1:Float, y1:Float, x2:Float, y2:Float)Local i := x1-x2Local j := y1-y2Return Sqrt(i*i + j*j)End' Entity inside the "world" has x and y fieldsClass EntityField x:FloatField y:Float' entity is given a random coordinate in the worldMethod New()Self.x = Rand()Self.y = Rand()End' Get Distance from Self to another entitiy in the "world"Method DistStruct(e:Entity)CalculateDistance(Self, e) ' passing Vec2f structs with coordinatesEndMethod DistStructGet(e:Entity)CalculateDistance(GetVec2f(), e.GetVec2f()) ' passing Vec2f structs with coordinatesEndMethod DistPrimsGet(e:Entity)CalculateDistance(GetX(), GetY(), e.GetX(), e.GetY()) ' passing primitive coordinate valuesEndMethod DistPrims(e:Entity)CalculateDistance(x, y, e.x, e.y) ' passing primitive coordinate valuesEndMethod GetX:Float()Return xEndMethod GetY:Float()Return yEndMethod To:Vec2f()Return New Vec2f(x, y)EndMethod GetVec2f:Vec2f()Return New Vec2f(GetX(), GetY())EndEnd' The test involves calculating distances from each entity (in an array of entitties) to every entity'>in the same array.Const n := 5000 ' size of entitiy arrayFunction Main()Local ents:Entity[] = New Entity[n]For Local i:=0 Until nents[i] = New Entity()EndLocal t := Millisecs()'' CREATE AND PASS STRUCT TO DISTANCE CALC. FUNCTION ''For Local i:=0 Until nFor Local j:=0 Until nents[i].DistStruct(ents[j])EndEndPrint "Time DistStruct = " + (Millisecs()-t)t = Millisecs()'' CREATE AND PASS STRUCT (created using getters) TO DISTANCE CALC. FUNCTION ''For Local i:=0 Until nFor Local j:=0 Until nents[i].DistStructGet(ents[j])EndEndPrint "Time DistStruct (w/ getter) = " + (Millisecs()-t)t = Millisecs()'' PASS PRIMITIVES TO DISTANCE CALC. FUNCTION ''For Local i:=0 Until nFor Local j:=0 Until nents[i].DistPrims(ents[j])EndEndPrint "Time DistPrims = " + (Millisecs()-t)t = Millisecs()'' PASS PRIMITIVES (from getter) TO DISTANCE CALC. FUNCTION ''For Local i:=0 Until nFor Local j:=0 Until nents[i].DistPrimsGet(ents[j])EndEndPrint "Time DistPrims (w/ getter) = " + (Millisecs()-t)EndFunction Rand:Float()Return Rnd(100)EndI think for my specific case I’d rather be creating structs than objects since they are a one time use thing and creating objects would drastically increase the load on the GC, right?
I am just using “Rect” structs as a more convenient way to pass primitives (i.e. coordinates and dimensions for hit testing) and just wanted to make sure I am not making my game terribly inefficient in the process.
Thanks! I suppose it always pays to check the docs and forums.
So creating a struct and passing it around instead of primitives is basically the same as both the primitives and structs are “pass by value”.
-
AuthorPosts