Is there in MicroPython any difference in time and memory effciency between
a) having a module with global symbols and functions using these symbols and
b) putting that in a class with class methods and symbols in a class.
Option b) obviously looks better and there is not the trap of forgetting to define a symbol as global when it is ought to be accessed in a function.
Efficiency global symbols vs. class members
Re: Efficiency global symbols vs. class members
I think that if you care about such small details (in a small, tight loop somewhere), you should be using local variables -- those are the fastest to access, as they only involve one lookup in the local namespace.
A class attribute access is a lookup in the local namespace for the "self", second, failed, lookup in the object's namespace for the attribute, and then a third lookup in the class' namespace (or more, if it's inherited from somewhere).
A global variable is a series of failed lookups in all nested namespaces, followed by a successful lookup in the global namespace.
A class attribute access is a lookup in the local namespace for the "self", second, failed, lookup in the object's namespace for the attribute, and then a third lookup in the class' namespace (or more, if it's inherited from somewhere).
A global variable is a series of failed lookups in all nested namespaces, followed by a successful lookup in the global namespace.
Re: Efficiency global symbols vs. class members
Is that also true if the variable was declared as global?A global variable is a series of failed lookups in all nested namespaces,
Besides that, I faintly remember a statement that in MicroPython class variables are accessed by an index, compared to global variables which are searched by name. Butt I cannot find that again.
Re: Efficiency global symbols vs. class members
I don't know the specifics of MicroPython, what I wrote applies to any Python. There might be some variations in details and optimizations depending on the implementation, of course -- that's why you usually don't do such micro-optimizations, as they might prove slower once the implementation changes. If you need a tight loop with good speed, assign everything to local variables and only use those inside the loop. Otherwise it shouldn't matter.
Re: Efficiency global symbols vs. class members
Thanks @deshipu. That's clear. In my case I have a lot of information to be shared between functions, especially operation mode settings, and one of them is an ISR. The code runs with sufficient speed, so it's more a matter of style and interest.If you need a tight loop with good speed, assign everything to local variables and only use those inside the loop.