Types vs. Banks: Speed vs. Space?

Blitz3D Forums/Blitz3D Programming/Types vs. Banks: Speed vs. Space?

Adam Novagen(Posted 2009) [#1]
Okay, here's a rather more advanced-system type question than I usually ask.

Let's say I have a level with a preset number of enemies that don't respawn, like in a Mario game or something. Each enemy has four variables:

Kind of enemy
X & Y position
Status of enemy (live, dead, etc)

The normal thing to do would be to use Types, with something like:

Type Enemy
    Field X,Y
    Field EnemyType,Status
End Type

And then update them with a For...Each...Next loop:

For Enemy.Enemy = Each Enemy
    If Enemy\Status = Alive ;Draw the correct enemy image from an image strip
        DrawImage Enemies,Enemy\X,Enemy\Y,Enemy\EnemyType
    EndIf
Next

In this situation, however, I could also use a bank in the same way, and go through the enemies with a For...Next loop, something like:

For CurrentEnemy = 0 To NumOfEnemies - 1
    ;Get enemy data
    Local EnemyStatus = PeekInt(EnemyBank,CurrentEnemy * 16)
    Local EnemyX      = PeekInt(EnemyBank,(CurrentEnemy * 16) + 4)
    Local EnemyY      = PeekInt(EnemyBank,(CurrentEnemy * 16) + 8)
    Local EnemyType   = PeekInt(EnemyBank,(CurrentEnemy * 16) + 12)

    If EnemyStatus = Alive;Draw the enemy
        DrawImage Enemies,EnemyX,EnemyY,EnemyType
    EndIf
Next

Both these solutions could work equally well from a code standpoint.

My question is this: would the Bank method use less or more RAM to store the enemy data than the traditional Type method? And would it be faster or slower?


Stevie G(Posted 2009) [#2]
I suspect that the types will likely take slightly more RAM to store as you would also have a pointer to the type instance. Both methods would probably show negligible difference in speed. I personally think that the type method is better as it's more elegant and easier to manage.


Adam Novagen(Posted 2009) [#3]
Yeah, I prefer Types too, I was simply wondering what kind of performance benefits there might be with the other method. I'll probably just stick with Types.


Gabriel(Posted 2009) [#4]
Tim Sweeney was famously quoted as saying (I think the subject was moving from C++ to C# for game development) that he would gladly sacrifice 10% of performance for a 10% productivity gain. This, to me, is just such an issue.


boomboom(Posted 2009) [#5]
Really don't worry about this. Lots of people can get hung up about what to use, floats, ints, types....etc for storage speed issues, but really just go with whats easy to use.

Any gains you make in how much ram you use can so easily be negated by including 1 extra texture file.

Performance perfection is never what will sell a game.


jfk EO-11110(Posted 2009) [#6]
I think banks are much faster. Anyone want to make a test?

Alternatively to your one bank for all entities, you could make your custom types made of banks, eg.
dim a(1000)
a(0)=createbank(16)
and so on, so you don't have to multiply by 16, but you can access a variable directly by bank-handle and offset.


Ross C(Posted 2009) [#7]
I would agree, but it depends on what your using them for. I did a pathfinding algo with types and it was much slower than the one i did with array, due to the creation and deletion of lots of types, and cycling through them.


Adam Novagen(Posted 2009) [#8]
Hmm... based on this info, I might start using arrays & banks for things like particles & frags; might be a significant boost!


ZJP(Posted 2009) [#9]
Bank (or array). Easy access to data with a dll

JP


Ross C(Posted 2009) [#10]
Not to be too picky, but i'd stick to types for particle, just for ease of use. And you can recycle types, so you don't need to constantly create and destroy them. The reason i didn't do this of pathfinding, is each type had a reference to the next one, so they were all linked.


ZJP(Posted 2009) [#11]
I do a "array version" of Particle Candy. This version is faster than the "type version". The objective was the translation of the source Blitz to C++ (for the SDK). The "array version" use more space. Sorry Google translate ;-)
JP


jfk EO-11110(Posted 2009) [#12]
Banks may be faster to some degree, but with types there's a lot you can do wrong, from a speed point of view. I've seen code searching for a certain index in the fields of types, slow as hell, where in an array indexed access is immediate.