line_profiler
Line-by-line profiling for Python
I've started looking for bottlenecks in Python using line_profiler
. Right now, I am doing that by running
kernprof -l -v myFile.py
However, the unit of time seems to be at 1e-6
, which lead to output results such as 132329040
. How can I increase the time interval to make the output more readable for larger time deltas?
Source: (StackOverflow)
I'm trying to use the line_profiler
library to profile some Python code. However, when I run the command:
kernprof -l -v bots\main.py
I get the following error:
Traceback (most recent call last):
File "C:\Python34\Scripts\kernprof-script.py", line 9, in <module>
load_entry_point('line-profiler==1.0', 'console_scripts', 'kernprof')()
File "C:\Python34\lib\site-packages\kernprof.py", line 221, in main
execfile(script_file, ns, ns)
File "C:\Python34\lib\site-packages\kernprof.py", line 34, in execfile
exec_(compile(f.read(), filename, 'exec'), globals, locals)
File "bots\main.py", line 2, in <module>
from bots.scheduler import *
ImportError: No module named 'bots'
Clearly the line_profiler
isn't recognising the __init__.py
file I have in the directory /bots
(where all my scripts are). When I run main.py
normally it works fine. How can I get line_profiler
to acknowledge the __init__.py
module and behave appropriately?
Source: (StackOverflow)
I'm using IPython 1.2.1 and Python 3.4.0 on Ubuntu. I'm trying to use the line_profiler module to analyse an algorithm. I've done $ pip install line_profiler
. My ipython_config.py file is at ~/.config/ipython/profile_default, to which I've added the lines:
c.TerminalIPythonApp.extensions = ['line_profiler']
and
c.InteractiveShellApp.extensions = ['line_profiler']
$ kernprof -l myscript.py
works fine in the terminal, albeit the output is a bit of a mess, but when I execute the %lprun
magic in IPython I get "ERROR: Line magic function %lprun
not found." When I try import line_profiler
I get "ImportError: No module name 'line_profiler'".
Source: (StackOverflow)
I have seen such a program in accident, but I forget the name and can not find it anymore.
It works just like line profiler. Well, when I saw it, it's an article comparing it with line profiler. The results are more friendly. They are like
main 100%
-fun1 95%
-subfun1 80%
...
-fun2 5%
-subfun1 4%
...
By result like this, I can know where subfun1 is called most.
Source: (StackOverflow)
I'm trying to profile my code and I cannot figure out why kernprof isn't working. I've run examples and they work just fine however my code just hangs. Here is my code:
import networkx as nx
from itertools import combinations
u = ['a','b','c','d']
pc = ['1,23,6,12',
'2,51,42',
'3',
'72,7,2,8']
@profile
def stuff(users, pixels_csv):
network = nx.Graph()
for user, pixels in zip(users, pixels_csv):
pixels = pixels.split(',')
pixel_combinations = combinations(pixels, 2)
for edge in pixel_combinations:
e0 = edge[0]
e1 = edge[1]
network.add_edge(e0, e1)
try:
if (e0, e1) in network.edges():
network[e0][e1]['weight'] += 1
except KeyError:
network[e0][e1]['weight'] = 1
for node in network.nodes():
if node in pixels:
try:
if user not in network.node[node]['unique_users']:
network.node[node]['unique_users'].append(user)
except KeyError:
network.node[node]['unique_users'] = [user]
return network.nodes()
stuff(u, pc)
I've tried the following commands and nothing happens, net.py in on my desktop.
comp:Desktop k$ kernprof -l -v net.py
comp:Desktop k$ python /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/kernprof.py -l -v net.py
this example works with this command:
comp:Desktop k$ python /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/kernprof.py -l -v primes.py
file called: primes.py
@profile
def primes(n):
if n==2:
return [2]
elif n<2:
return []
s=range(3,n+1,2)
mroot = n ** 0.5
half=(n+1)/2-1
i=0
m=3
while m <= mroot:
if s[i]:
j=(m*m-3)/2
s[j]=0
while j<half:
s[j]=0
j+=m
i=i+1
m=2*i+3
return [2]+[x for x in s if x]
primes(100)
Source: (StackOverflow)