python ix函数_内联函数和调用函数之间的Python评测差异

本文探讨了一个名为`getNeighbors`的Python函数,该函数用于从邻接矩阵中获取邻域信息。在10kx10k的邻接矩阵中,内联执行代码只需0.02秒,但调用函数形式则导致了0.5秒的额外开销。通过替换为随机数生成操作,函数调用时间降低到0.002秒。性能分析显示,大部分时间消耗在`sum`操作上,但该操作本身并不昂贵。这引发了一个问题:为何函数调用会有如此大的性能差异。
摘要由CSDN通过智能技术生成

我有这个代码:def getNeighbors(cfg, cand, adj):

c_nocfg = np.setdiff1d(cand, cfg)

deg = np.sum(adj[np.ix_(cfg, c_nocfg)], axis=0)

degidx = np.where(deg) > 0

nbs = c_nocfg[degidx]

deg = deg[degidx]

return nbs, deg

它从邻接矩阵中检索邻域(以及它们在cfg中由节点跨越的子图中的度)。

内联代码提供了合理的性能(大约10kx10k邻接矩阵作为布尔数组,10k个候选在cand中,子图cfg跨越500个节点):0.02s

但是,调用getNeighbors函数会导致大约0.5s的开销

嘲笑

^{2}$

与deg = np.random.randint(500, size=c_nocfg.shape[0])

将函数调用的运行时间降低到0.002s。

有人能解释一下是什么导致了我的案例中巨大的开销——毕竟,sum操作本身并不太昂贵。在

分析输出ncalls tottime percall cumtime percall filename:lineno(function)

1 0.466 0.466 0.488 0.488 /home/x/x/x/utils/benchmarks.py:15(getNeighbors)

1 0.000 0.000 0.019 0.019 /usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:1621(sum)

1 0.000 0.000 0.019 0.019 /usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:23(_sum)

1 0.019 0.019 0.019 0.019 {method 'reduce' of 'numpy.ufunc' objects}

1 0.000 0.000 0.002 0.002 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:410(setdiff1d)

1 0.000 0.000 0.001 0.001 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:284(in1d)

2 0.001 0.000 0.001 0.000 {method 'argsort' of 'numpy.ndarray' objects}

2 0.000 0.000 0.001 0.000 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:93(unique)

2 0.000 0.000 0.000 0.000 {method 'sort' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.where}

4 0.000 0.000 0.000 0.000 {numpy.core.multiarray.concatenate}

2 0.000 0.000 0.000 0.000 {method 'flatten' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/numpy/lib/index_tricks.py:26(ix_)

5 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py:392(asarray)

5 0.000 0.000 0.000 0.000 {numpy.core.multiarray.array}

1 0.000 0.000 0.000 0.000 {isinstance}

2 0.000 0.000 0.000 0.000 {method 'reshape' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 {range}

2 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}

2 0.000 0.000 0.000 0.000 {issubclass}

2 0.000 0.000 0.000 0.000 {method 'ravel' of 'numpy.ndarray' objects}

6 0.000 0.000 0.000 0.000 {len}

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

内联版本:ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.019 0.019 /usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:1621(sum)

1 0.000 0.000 0.019 0.019 /usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py:23(_sum)

1 0.019 0.019 0.019 0.019 {method 'reduce' of 'numpy.ufunc' objects}

1 0.000 0.000 0.002 0.002 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:410(setdiff1d)

1 0.000 0.000 0.001 0.001 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:284(in1d)

2 0.001 0.000 0.001 0.000 {method 'argsort' of 'numpy.ndarray' objects}

2 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/numpy/lib/arraysetops.py:93(unique)

2 0.000 0.000 0.000 0.000 {method 'sort' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.where}

4 0.000 0.000 0.000 0.000 {numpy.core.multiarray.concatenate}

2 0.000 0.000 0.000 0.000 {method 'flatten' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/numpy/lib/index_tricks.py:26(ix_)

5 0.000 0.000 0.000 0.000 /usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py:392(asarray)

5 0.000 0.000 0.000 0.000 {numpy.core.multiarray.array}

1 0.000 0.000 0.000 0.000 {isinstance}

2 0.000 0.000 0.000 0.000 {method 'reshape' of 'numpy.ndarray' objects}

1 0.000 0.000 0.000 0.000 {range}

2 0.000 0.000 0.000 0.000 {method 'ravel' of 'numpy.ndarray' objects}

2 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}

2 0.000 0.000 0.000 0.000 {issubclass}

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

6 0.000 0.000 0.000 0.000 {len}

测试样本数据:np.random.seed(0)

adj = np.zeros(10000*10000, dtype=np.bool)

adj[np.random.randint(low=0, high=10000*10000+1, size=100000)] = True

adj = adj.reshape((10000, 10000))

cand = np.arange(adj.shape[0])

cfgs = np.random.choice(cand, size=500)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值