Efficiency global symbols vs. class members

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Efficiency global symbols vs. class members

Post by Roberthh » Wed Feb 08, 2017 12:10 pm

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Efficiency global symbols vs. class members

Post by deshipu » Wed Feb 08, 2017 4:34 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Efficiency global symbols vs. class members

Post by Roberthh » Wed Feb 08, 2017 7:39 pm

A global variable is a series of failed lookups in all nested namespaces,
Is that also true if the variable was declared as global?
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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Efficiency global symbols vs. class members

Post by deshipu » Wed Feb 08, 2017 9:48 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Efficiency global symbols vs. class members

Post by Roberthh » Thu Feb 09, 2017 6:42 am

If you need a tight loop with good speed, assign everything to local variables and only use those inside the loop.
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.

Post Reply