我正在尝试分析我的代码,但我无法弄清楚为什么kernprof不起作用。我已经运行了一些示例,它们工作得很好,但我的代码只是挂起。这是我的代码:
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)
我尝试了以下命令,没有任何反应,我的桌面上有net.py.
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
此示例适用于此命令:
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
文件名为: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)