简体   繁体   English

Python 子模块 class 实例生成

[英]Python sub-module class instance generation

I want to place the generation of random numbers into a sub-module ("rv_gen.py");我想将随机数的生成放入一个子模块(“rv_gen.py”); it is not clear to me whether an instance of the random number array (or of the class?) is generated upon the import statement, or only when assigning this to the variable rvclass (in the main module)?我不清楚是否在导入语句时生成随机数数组(或 class?)的实例,还是仅在将其分配给变量rvclass (在主模块中)时生成?

Puzzling is the timing: the output (see below) differs after I change the size of the random number array, and takes unexpectedly long (I expect a few sec for 10M normal rvs):令人费解的是时间:output(见下文)在我更改随机数数组的大小后有所不同,并且花费了意外的时间(我预计 10M 正常 rvs 需要几秒钟):

After a change (and saving) of "rv_gen", the first three lines of the output below until the second print('rvs generation finished!') are printed immediately, however the next print statement 'Time for import:...' takes several minutes (not just "3.1 seconds" as stated).更改(并保存)“rv_gen”后,立即打印下面 output 的前三行直到第二次print('rvs generation finished!') ,但是下一个打印语句'Time for import:...'需要几分钟(不仅仅是所说的“3.1秒”)。 Then the remaining three prints follow immediately.紧接着剩下的三张照片紧随其后。 I had expected the main time lag before each 'rvs generation finished' print...?我曾预计每个“rvs generation完成”打印之前的主要时间延迟......?

If I just rerun the main module without any change to the sub-module, the output is similar except missing the first line print('rvs generation finished!') below, and does not take additional minutes before the statement now reading Time for import: 0.3 seconds .如果我只是重新运行主模块而不对子模块进行任何更改,则 output 类似,只是缺少下面的第一行print('rvs generation finished!') ,并且在现在读取Time for import: 0.3 seconds的语句之前不需要额外的分钟Time for import: 0.3 seconds

Main module "main.py":主模块“main.py”:

import timeit
lastt   = timeit.default_timer()
import rv_gen
thist   = timeit.default_timer(); print('Time for import: %4.1f seconds.' % ( thist-lastt)) ; lastt = thist     
rvclass = rv_gen.s
thist   = timeit.default_timer(); print('Time for assignment: %4.1f seconds.' % ( thist-lastt)) ; lastt = thist     
print(rvclass.rans[:5])

Module "rv_gen.py":模块“rv_gen.py”:

import numpy as np
class struct:
    pass
s      = struct()
s.rans = np.random.normal(size=1000*1000*50) 
print('rvs generation finished!')

Output (after updating rv_gen.py): Output(更新 rv_gen.py 后):

rvs generation finished!
Reloaded modules: rv_gen
rvs generation finished!
Time for import:  3.1 seconds.
Time for assignment:  0.0 seconds.
[ 0.925 -0.728  2.387 -0.683 -0.021]

Edit: Now getting a "MemoryError": I just reran the code after changing the random array size to 10M, and got the following output (again the first rvs generation finished! printout appearing immediately, the rest after about 3 minutes):编辑:现在得到一个“MemoryError”:我只是在将随机数组大小更改为 10M 后重新运行代码,并得到以下 output(再次完成第一rvs generation finished!打印输出立即出现,rest 大约 3 分钟后):

rvs generation finished!
[autoreload of rv_gen failed: Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 245, in check
    superreload(m, reload, self.old_objects)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 450, in superreload
    update_generic(old_obj, new_obj)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 387, in update_generic
    update(a, b)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 357, in update_class
    update_instances(old, new)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 312, in update_instances
    update_instances(old, new, obj.__dict__, visited)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 317, in update_instances
    update_instances(old, new, obj, visited)
  File "C:\Anaconda\lib\site-packages\IPython\extensions\autoreload.py", line 302, in update_instances
    visited.update({id(obj):obj})
MemoryError
]
Reloaded modules: rv_gen
rvs generation finished!
Time for import:  0.4 seconds.
Time for assignment:  0.0 seconds.
[-0.091  1.087 -0.329  0.359 -1.884]

I am using Anaconda Spyder 3.3.6 and IPython.我正在使用 Anaconda Spyder 3.3.6 和 IPython。

the statements in the rv_gen would run upon the time of import. rv_gen 中的语句将在导入时运行。 There is no much gap between your prints and even the generation of 100k random numbers will take no time in morden PCs您的打印之间没有太大的差距,即使在现代 PC 中生成 100k 随机数也不会花费时间

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

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