简体   繁体   English

4-D numpy数组字典:如何优化?

[英]Dictionary of 4-D numpy array: how to optimize?

I have a problem of performances in inizializing a dictionary of 4-D numpy tensor. 我有一个关于4-D numpy tensor字典的表现问题。

I have a list of coefficients names: 我有一个系数名称列表:

cnames = ['CN', 'CM', 'CA', 'CY', 'CLN' ...];

that is not fixed-size (it depends from upper code). 这不是固定大小(取决于上层代码)。 For each coefficient i have to generate a 4-D tensor [nalpha X nmach X nbeta X nalt] of zeros (for preallocation purposes), so I do: 对于每个系数,我必须生成零的4-D张量[nalpha X nmach X nbeta X nalt](用于预分配目的),所以我这样做:

#Number of coefficients
numofc = len(cnames);

final_data = {};
#I have to generate <numofc> 4D matrixes
for i in range(numofc):
    final_data[cnames[i]]=n.zeros((nalpha,nmach,nbeta,nalt));

each index is an integer between 10 and 30. each index is an integer between 100 and 200 每个索引是10到30之间的整数。 每个索引是100到200之间的整数

This takes like 4 minutes. 这需要4分钟。 How can I speed up this? 我怎样才能加快这个速度? Or am I doing something wrong? 或者我做错了什么?

The code you posted should not take 4 minutes to run (unless cnames is extremely large or you have very little RAM and is forced to use swap space). 您发布的代码不应该运行4分钟(除非cnames非常大或者您的RAM很少并且被迫使用交换空间)。

import numpy as np

cnames = ['CN', 'CM', 'CA', 'CY', 'CLN']*1000
nalpha,nmach,nbeta,nalt = 10,20,30,40
#Number of coefficients
numofc = len(cnames)

final_data = {}
#I have to generate <numofc> 4D matrixes
for i in range(numofc):
    final_data[cnames[i]]=np.zeros((nalpha,nmach,nbeta,nalt))

Even if cnames has 5000 elements, it should still take only on the order of a couple seconds: 即使cnames有5000个元素,它仍然只需要几秒钟的时间:

% time test.py
real    0m4.559s
user    0m0.856s
sys 0m3.328s

The semicolons at the end of statements suggests you have experience in some other language. 陈述末尾的分号表明您有其他语言的经验。 Be careful of translating commands from that language line-by-line into NumPy/Python. 注意将该语言中的命令逐行转换为NumPy / Python。 Coding in NumPy as one would in C is a recipe for slowness. NumPy中的编码就像在C中一样,是慢速的一个秘诀。

In particular, try to avoid updating elements in an array element-by-element. 特别是,尽量避免逐个元素地更新数组中的元素。 This works fine in C, but is very slow with Python. 这在C中运行良好,但在Python中运行速度很慢。 NumPy achieves speed by delegating to functions coded in Fortran or Cython or C or C++. NumPy通过委托使用Fortran或Cython或C或C ++编写的函数来实现速度。 By updating arrays element-by-element you are using Python loops which are not as fast. 通过逐个元素更新数组,您使用的Python循环速度并不快。

Instead, try to rephrase your computation in terms of operations on whole arrays (or at least, slices of arrays). 相反,尝试根据整个数组(或至少是数组切片)的操作来重新计算您的计算。

I have probably speculated too much on the cause of the problem. 我可能过多地猜测问题的原因。 You need to profile your code , and then, if you want more specific help, post the result of the profile plus the problematic code (most helpfully in the form of an SSCCE ). 您需要对代码进行概要分析 ,然后,如果您需要更具体的帮助,请发布概要文件的结果以及有问题的代码(最有帮助的是以SSCCE的形式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM