【问题标题】:Measuring number of floating-point operations in python algorithm在python算法中测量浮点运算的数量
【发布时间】:2018-07-07 08:43:34
【问题描述】:

我有一个用 Python 2.7 实现的数据处理算法,我需要在嵌入式系统上移动它(让它是微控制器或更高级的板)。要选择硬件,我必须知道执行了多少次浮点运算以及总共使用了多少内存。

如何有效地确定这些?

【问题讨论】:

    标签: python algorithm python-2.7 profiling microcontroller


    【解决方案1】:

    要计算操作次数,您可以执行以下操作

    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?

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2013-06-12
      • 2016-08-23
      • 1970-01-01
      相关资源
      最近更新 更多