我很好奇http://docs.python.org/2/library/itertools.html#itertools.imap中的一个语句,即它描述了
sum(imap(operator.mul, vector1, vector2))
作为一个有效的点产品。我的理解是,imap提供的是一个生成器而不是一个列表,虽然我知道如果只考虑前几个元素和周围的sum(),它将如何更快/消耗更少的内存,但我看不出它的行为有什么不同于:
sum(map(operator.mul, vector1, vector2))
Tags:
当你开始增加迭代内容的大小时,
map
和imap
之间的区别就变得很明显了:注意,在Python 3中,内置的
map
的行为类似于Python 2的itertools.imap
(因为不再需要而被删除)。为了获得“旧的map
”行为,您可以使用list(map(...))
,这是另一种可视化Python 2的itertools.imap
和map
之间差异的好方法。另一件需要注意的事情是,“使用更少的内存”通常也意味着“运行得更快”。lazy(迭代器)版本一计算出每个产品,就将其消耗掉,并将其添加到运行总和中。产品和运行总和几乎都肯定在一级缓存中。如果首先计算所有产品,那么根据元素的数量,可以确定计算的第一个产品将从一级缓存中踢出,然后从二级缓存中踢出,并且。。。因此,当第二次将它们相加时,所有的产品在内存层次结构中都很低(最极端的是,必须从分页文件中读取)。
但我不清楚你所说的“看不出它的行为有什么不同”。无论哪种方式,最终的计算结果都是相同的。
第一行将逐个计算累积项的总和。第二个将首先计算整个点积,然后,将整个结果存储在内存中,然后继续计算和。因此内存复杂性增加。
相关问题 更多 >
编程相关推荐