Skip to main content

Python is SLOW - How to speed it up - 1

Python is slow. 

For most cases this doesn't really matter as the development time is more of a factor. However on low CPU power devices (phones) anything is significant.

First tip on how to speed up python, is to use Atom!  Just install with pip.  Atom speeds up object creation by about 50% per property (ex an object with 4 properties is about 2x faster, 8 properties 4x, etc...) . I've seen significant performance increases simply by switching in my testing. See the simple example below:
from atom.api import Atom, ForwardInstance, Callable, Unicode, Tuple, Bool, Int,  Instance, set_default, Property
class TestAtom(Atom):
    i = Int(1)
    a = Unicode("abc")
    b = Tuple(default=(1,2,3))
    c = Bool()
    f = Callable(default=lambda: "test")
    
class TestObj(object):
    def __init__(self):
        self.i = 1
        self.a = 'abc'
        self.b = (1,2,3)
        self.c = False
        self.f = lambda: "test"
        
%timeit TestAtom()
The slowest run took 27.35 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 218 ns per loop
%timeit TestObj()
The slowest run took 11.07 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 732 ns per loop

As you can see this simple example the atom object is created almost 4x faster than the python object. The speed increase scales linearly with the number of attributes in the object.  Also the atom object uses about 1/4 the memory (which probably explains the speed increase).


from pympler.asizeof import asizeof

a = TestAtom()
b = TestObj()
asizeof(a)
Out[20]: 176
asizeof(b)
Out[21]: 760
Cheers!

Comments

Popular posts from this blog

Kivy vs React-Native for building cross platform mobile apps

I've built three apps now using Kivy and one with React-Native, just wanted to share my thoughts on both.

Just a warning, I am strongly biased towards python and this is all based on opinion and experience and is thus worth what you pay for it. I don't claim to be an expert in either of these, just have worked with each for several months.  If something is incorrect I'd love to hear advice.

Kivy
Demo of one of the apps



Pros:
Nice to be able to run natively on the desktop WITHOUT a simulatorPython is easy to work withUse (almost) any python libraryVery easy to create custom widgetsKivy properties and data binding just work. Way nicer than React's "state" / flux / redux whatever you want to call it (stupid?). Native interfaces (pyjnius) and (pyobjc)Runs and feels pretty smooth Cons:Default widget toolkit looks like Android 4.4. Requiring you use your own widgets or a theming kit like KivyMD if styling bothers youCreating dynamic widgets declaratively is not yet s…

Control Systems in Python - Part 1 - Bode and Step Response

I hate matlab with passion, yet sadly, nearly everyone uses it.  I'm a fan of Python and open source stuff so here's a simple article on how to do some common control systems stuff in Python.

First we need to make sure the environment is setup.
Install IPython (or you can use any other python shell, but a unicode supported shell is preferred)Install python-control (numpy, scipy)Install sympy
These should do if your on Ubuntu/debian:

sudo apt-get install python-sympy python-numpy python-scipy python-matplotlib ipython
Then you need to install python control, see How to download and install python-control
Intro to using SympyOpen ipython and run the following:

import sympy from sympy import * sympy.init_printing() s = Symbol('s')

Now we can do things like define transfer functions using the symbolic variable s.


We can expand the bottom using the .simplify() method

and we can do something more complex like...
which is really nice because it does all the multiplication for us... and it’…