【发布时间】:2018-07-07 08:43:34
【问题描述】:
我有一个用 Python 2.7 实现的数据处理算法,我需要在嵌入式系统上移动它(让它是微控制器或更高级的板)。要选择硬件,我必须知道执行了多少次浮点运算以及总共使用了多少内存。
如何有效地确定这些?
【问题讨论】:
标签: python algorithm python-2.7 profiling microcontroller
我有一个用 Python 2.7 实现的数据处理算法,我需要在嵌入式系统上移动它(让它是微控制器或更高级的板)。要选择硬件,我必须知道执行了多少次浮点运算以及总共使用了多少内存。
如何有效地确定这些?
【问题讨论】:
标签: python algorithm python-2.7 profiling microcontroller
要计算操作次数,您可以执行以下操作
class N:
ops = 0
def __init__(self, x):
self.x = x
def __add__(self, rhs):
N.ops += 1
return N(self.x + rhs.x)
result = N(1) + N(2)
assert result.x == 3
assert N.ops == 1
并用 N(float)s 替换所有浮点数。
有关内存使用情况,请参阅How do I profile memory usage in Python?
【讨论】: