performance - improving speed of Python module import -


the question of how speed importing of python modules has been asked (speeding python "import" loader , python -- speed imports?) without specific examples , has not yielded accepted solutions. therefore take issue again here, time specific example.

i have python script loads 3-d image stack disk, smooths it, , displays movie. call script system command prompt when want view data. i'm ok 700 ms takes smooth data comparable matlab. however, takes additional 650 ms import modules. user's perspective python code runs @ half speed.

this series of modules i'm importing:

import numpy np import matplotlib.pyplot plt import matplotlib.animation animation import scipy.ndimage import scipy.signal import sys import os 

of course, not modules equally slow import. chief culprits are:

matplotlib.pyplot   [300ms] numpy               [110ms] scipy.signal        [200ms] 

i have experimented using from, isn't faster. since matplotlib main culprit , it's got reputation slow screen updates, looked alternatives. 1 pyqtgraph, takes 550 ms import.

i aware of 1 obvious solution, call function interactive python session rather system command prompt. fine it's matlab-like, i'd prefer elegance of having function available system prompt.

i'm new python , i'm not sure how proceed @ point. since i'm new, i'd appreciate links on how implement proposed solutions. ideally, i'm looking simple solution (aren't all!) because code needs portable between multiple mac , linux machines.

you build simple server/client, server running continuously making , updating plot, , client communicating next file process.

i wrote simple server/client example based on basic example socket module docs: http://docs.python.org/2/library/socket.html#example

here server.py:

# expensive imports import numpy np import matplotlib.pyplot plt import matplotlib.animation animation import scipy.ndimage import scipy.signal import sys import os  # echo server program import socket  host = ''                 # symbolic name meaning available interfaces port = 50007              # arbitrary non-privileged port s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((host, port)) s.listen(1) while 1:     conn, addr = s.accept()     print 'connected by', addr     data = conn.recv(1024)     if not data: break     conn.sendall("plotting:" + data)     # update plot     conn.close() 

and client.py:

# echo client program import socket import sys  host = ''    # remote host port = 50007              # same port used server s = socket.socket(socket.af_inet, socket.sock_stream) s.connect((host, port)) s.sendall(sys.argv[1]) data = s.recv(1024) s.close() print 'received', repr(data) 

you run server:

python server.py 

which imports, client sends via socket filename of new file plot:

python client.py mytextfile.txt 

then server updates plot.

on machine running imports take 0.6 seconds, while running client.py 0.03 seconds.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -