local vs global vs field
BlitzMax Forums/BlitzMax Programming/local vs global vs field
| ||
in a function locals are faster than globals, but what about field access of a local object? how do objects compare to locals |
| ||
Fields in objects are like globals. You could store them in locals when used heavy duty. |
| ||
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? |
| ||
are private globals any faster than normal globals No. But private globals are annoying when you need access to the object from another module... |
| ||
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. |
| ||
in a function locals are faster than globals Are they?? |
| ||
Are they?? Yes, always. |
| ||
I suppose you mean globals like the ones you declare outside of a function? |
| ||
Why? And why does it make a difference if they are inside a function or not? |
| ||
I don't follow. You're always in a function, so how do you propose to have a local without being in one? |
| ||
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. |
| ||
Their addresses are in registers though (at least I assume they are), which would still be faster than pulling that out of the heap. |
| ||
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. |