矢量化算法
Vectorizing Algorithms
In Numerical Methods in Engineering with Python 3,作者:Jaan Kiusalaas,第 25 页,作者以两种方式求解求和表达式:
第一种方法:使用循环。
第二种方法:通过从 numpy 导入 arange 的矢量化版本。
作者指出"The vectorized algorithm executes much faster, but uses more memory."
任何人都可以解释矢量化算法的含义以及为什么矢量化算法执行得更快,但使用更多内存?
Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.
[What is “vectorization”?]
矢量化代码更快,因为多个操作是并行执行的。 相比之下,在循环版本中,这些操作是顺序执行的,一个接一个
矢量化代码使用更多内存,因为所有操作的数据,它们是并行执行的,必须加载到内存中。相比之下,对于循环版本,只需要加载一次操作(当前正在执行)的数据。
这是计算机科学中的典型权衡,称为 Space–time tradeoff。
In Numerical Methods in Engineering with Python 3,作者:Jaan Kiusalaas,第 25 页,作者以两种方式求解求和表达式:
第一种方法:使用循环。
第二种方法:通过从 numpy 导入 arange 的矢量化版本。
作者指出"The vectorized algorithm executes much faster, but uses more memory." 任何人都可以解释矢量化算法的含义以及为什么矢量化算法执行得更快,但使用更多内存?
Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times. [What is “vectorization”?]
矢量化代码更快,因为多个操作是并行执行的。 相比之下,在循环版本中,这些操作是顺序执行的,一个接一个
矢量化代码使用更多内存,因为所有操作的数据,它们是并行执行的,必须加载到内存中。相比之下,对于循环版本,只需要加载一次操作(当前正在执行)的数据。
这是计算机科学中的典型权衡,称为 Space–time tradeoff。