C
Crypten
Guest
I have a question but let me give some context first.
I have a project with three distinct modes.
Each mode requires their own set of global variables that are unused by the other modes.
Along with that some of these global variables will be quite large in size. (Large Data Structures)
So, for the purpose of saving memory and for good practice, it is my understanding that the best practice is to store my "global but not really global to the whole program variables" in an object or objects.
That way I can create and remove the object/s as needed. (The hidden global object can only have variables added to it and modified as far as I know)
Simple enough.
But this brings me to my question.
How should variables like these be accessed?
I read through this thread: https://forum.yoyogames.com/index.php?threads/are-global-variables-worth-it.33787/
There was some interesting discussion on which variable variations were the fastest to read and write.
YoYo Staff member Russell gave a link to a project file he made for testing this: https://www.dropbox.com/s/5gmewcwmdjhyer8/TestVarTiming.yyz?dl=0
Compiling the above code I got these results:
GMS2
Instance Write - elapsed time=0.05us
Other Instance Write - elapsed time=0.08us
Object Write - elapsed time=0.06us
Global Write - elapsed time=0.06us
Local Write - elapsed time=0.05us
Instance Read - elapsed time=0.06us
Other Instance Read - elapsed time=0.09us
Object Read - elapsed time=0.07us
Global Read - elapsed time=0.06us
Local Read - elapsed time=0.06us
GMS1.4
Instance Write - elapsed time=0.06us
Other Instance Write - elapsed time=0.09us
Object Write - elapsed time=0.07us
Global Write - elapsed time=0.07us
Local Write - elapsed time=0.06us
Instance Read - elapsed time=0.07us
Other Instance Read - elapsed time=0.09us
Object Read - elapsed time=0.07us
Global Read - elapsed time=0.06us
Local Read - elapsed time=0.06us
(The code was written for GMS2 but I modified the syntax to compile in GMS1.4)
For reference:
Instance Write
Other Instance Write
Object Write
Global Write
Local Write
Instance Read
Other Instance Read
Object Read
Global Read
Local Read
Going from fastest to slowest the results roughly read
Local and Instance < Global and Object < Other Instance
But, before this project and results were posted in the thread above some community members were saying that they believed the best practice was to use the object id (Other Instance) when reading and writing to variables in another object. They believed that the Other Instance method would be faster than either the Global or Object methods and were surprised when the results showed otherwise. That thread never really reached a consensus or conclusion.
So I ask: When accessing objects that will never have more than one instance do you think their id should be used (Other Instance) or simply their name (Object)?
On a semi-related note. Did GMS2 patch 2.1.3.273 (released Wed, 13 Dec 2017) affect anything related to accessing object variables? That was the patch that added the Variables section to the Object editor. I don't believe so but let me know if anything has changed because of the patch.
Feel free to share any thoughts on this discussion.
I have a project with three distinct modes.
Each mode requires their own set of global variables that are unused by the other modes.
Along with that some of these global variables will be quite large in size. (Large Data Structures)
So, for the purpose of saving memory and for good practice, it is my understanding that the best practice is to store my "global but not really global to the whole program variables" in an object or objects.
That way I can create and remove the object/s as needed. (The hidden global object can only have variables added to it and modified as far as I know)
Simple enough.
But this brings me to my question.
How should variables like these be accessed?
I read through this thread: https://forum.yoyogames.com/index.php?threads/are-global-variables-worth-it.33787/
There was some interesting discussion on which variable variations were the fastest to read and write.
YoYo Staff member Russell gave a link to a project file he made for testing this: https://www.dropbox.com/s/5gmewcwmdjhyer8/TestVarTiming.yyz?dl=0
Compiling the above code I got these results:
GMS2
Instance Write - elapsed time=0.05us
Other Instance Write - elapsed time=0.08us
Object Write - elapsed time=0.06us
Global Write - elapsed time=0.06us
Local Write - elapsed time=0.05us
Instance Read - elapsed time=0.06us
Other Instance Read - elapsed time=0.09us
Object Read - elapsed time=0.07us
Global Read - elapsed time=0.06us
Local Read - elapsed time=0.06us
GMS1.4
Instance Write - elapsed time=0.06us
Other Instance Write - elapsed time=0.09us
Object Write - elapsed time=0.07us
Global Write - elapsed time=0.07us
Local Write - elapsed time=0.06us
Instance Read - elapsed time=0.07us
Other Instance Read - elapsed time=0.09us
Object Read - elapsed time=0.07us
Global Read - elapsed time=0.06us
Local Read - elapsed time=0.06us
(The code was written for GMS2 but I modified the syntax to compile in GMS1.4)
For reference:
Code:
var instance = instance_create_depth( 10, 10, -100, object0 );
var ten = 10;
Code:
foo = 10;
Code:
instance.foo = 10;
Code:
object0.foo = 10;
Code:
global.foo = 10;
Code:
var foofoo = 10;
Code:
foofoo = foo;
Code:
foofoo = instance.foo;
Code:
foofoo = object0.foo;
Code:
foofoo = global.foo;
Code:
foofoo = ten;
Local and Instance < Global and Object < Other Instance
But, before this project and results were posted in the thread above some community members were saying that they believed the best practice was to use the object id (Other Instance) when reading and writing to variables in another object. They believed that the Other Instance method would be faster than either the Global or Object methods and were surprised when the results showed otherwise. That thread never really reached a consensus or conclusion.
So I ask: When accessing objects that will never have more than one instance do you think their id should be used (Other Instance) or simply their name (Object)?
On a semi-related note. Did GMS2 patch 2.1.3.273 (released Wed, 13 Dec 2017) affect anything related to accessing object variables? That was the patch that added the Variables section to the Object editor. I don't believe so but let me know if anything has changed because of the patch.
Feel free to share any thoughts on this discussion.