local vs global vs field

BlitzMax Forums/BlitzMax Programming/local vs global vs field

slenkar(Posted 2010) [#1]
in a function locals are faster than globals,
but what about field access of a local object? how do objects compare to locals


jsp(Posted 2010) [#2]
Fields in objects are like globals.
You could store them in locals when used heavy duty.


slenkar(Posted 2010) [#3]
thanks for the reply, ive heard that if you have more than 6 locals in a function they cant fit in the registers of the CPU and it gets slow.

would it be quicker to do something in a method, so the fields are easily accessible? or would that be just as slow as a global?


Im using a function called Draw_map and when the coords and images are locals (function arguments) instead of globals it is a lot faster.

brl.max2d.mod has a private global object GC:Tmax2dGraphics where all the drawing information is kept (e.g. scale,rotation, color)
are private globals any faster than normal globals?


Brucey(Posted 2010) [#4]
are private globals any faster than normal globals

No. But private globals are annoying when you need access to the object from another module...


ImaginaryHuman(Posted 2010) [#5]
I found that accessing a field with mytype.fieldname is about the same speed as passing a variable to a function, if that helps at all. I had a bunch of methods and I wanted to turn them into functions outside of the type, and so instead of having get/set methods I just directly accessed the fields and it was as fast as trying to pass them. But not as fast as locals.


Czar Flavius(Posted 2010) [#6]
in a function locals are faster than globals
Are they??


N(Posted 2010) [#7]
Are they??
Yes, always.


ProfJake(Posted 2010) [#8]
I suppose you mean globals like the ones you declare outside of a function?


Czar Flavius(Posted 2010) [#9]
Why? And why does it make a difference if they are inside a function or not?


N(Posted 2010) [#10]
I don't follow. You're always in a function, so how do you propose to have a local without being in one?


Perturbatio(Posted 2010) [#11]
Just as a note, I think it's only simple typed locals (Int, Float, String) that are faster in functions since complex structures are too big for the stack or something.

I have a vague memory of Mark mentioning something about this.


N(Posted 2010) [#12]
Their addresses are in registers though (at least I assume they are), which would still be faster than pulling that out of the heap.


ImaginaryHuman(Posted 2010) [#13]
Yah I agree, the pointer to an instance of a type will be in a local if you put it there, but the fields will not, they are drawn from memory/cache. Using locals anywhere is faster than globals anywhere as globals are also stored in memory.

If I'm going to be using a variable several times, and it was stored in a field in memory, I typically read it into a local first and then reference the local and write any changes back out later.