小编dpy*_*yro的帖子

Python的总和vs. NumPy的numpy.sum

使用Python的本机sum函数和NumPy 之间在性能和行为上有什么不同numpy.sumsum适用于NumPy的数组,numpy.sum可以在Python列表上运行,它们都返回相同的有效结果(没有测试边缘情况,如溢出)但不同类型.

>>> import numpy as np
>>> np_a = np.array(range(5))
>>> np_a
array([0, 1, 2, 3, 4])
>>> type(np_a)
<class 'numpy.ndarray')

>>> py_a = list(range(5))
>>> py_a
[0, 1, 2, 3, 4]
>>> type(py_a)
<class 'list'>

# The numerical answer (10) is the same for the following sums:
>>> type(np.sum(np_a))
<class 'numpy.int32'>
>>> type(sum(np_a))
<class 'numpy.int32'>
>>> type(np.sum(py_a))
<class 'numpy.int32'>
>>> type(sum(py_a))
<class 'int'>
Run Code Online (Sandbox Code Playgroud)

编辑:我认为我的实际问题是numpy.sum在Python整数列表上使用比使用Python自己更快sum吗?

另外,使用Python整数与标量有 …

python performance numpy python-3.x

48
推荐指数
4
解决办法
4万
查看次数

在Python列表中存储Python对象与固定长度的Numpy数组

在做一些生物信息学工作时,我一直在思考将对象实例存储在Numpy数组而不是Python列表中的后果,但在我所做的所有测试中,性能在每个实例中都更糟.我正在使用CPython.有谁知道原因?

特别:

  • 使用固定长度数组numpy.ndarray(dtype=object)与常规Python列表有什么性能影响?我执行的初始测试表明,访问Numpy数组元素比通过Python列表迭代更慢,尤其是在使用对象方法时.
  • 为什么使用列表解析来实例化对象更快,例如[ X() for i in range(n) ]代替numpy.empty(size=n, dtype=object)
  • 每个的内存开销是多少?我无法测试这个.我的课程广泛使用__slots__,如果这有任何影响.

python performance numpy cpython python-3.x

10
推荐指数
1
解决办法
3563
查看次数

如何避免在Python文件输入库中进行缓冲

我在这里看到了这个问题,但是给出的答案在我的案例中没有用,并且被标记为重复.

我挖了源代码(/usr/lib/python3.2/fileinput.py)并看到它readlines(bufsize)在内部用于加载缓冲区.没有贝壳或其他管道的恶作剧.

python file-io readline buffering readlines

6
推荐指数
1
解决办法
1223
查看次数

自动加载常量时检测到循环依赖(Rails 4,Ruby 2)

CONFIGS/routes.rb中 ShuttersPaints是子资源Jobs.

resources :jobs do
    resources :shutters
    resources :paints
end
Run Code Online (Sandbox Code Playgroud)

app/models/job.rb A Job包含很多Shutters很多Paints.

class Job < ActiveRecord::Base
    has_many :shutters, dependent: :delete_all
    has_many :paints, dependent: :delete_all

    accepts_nested_attributes_for :shutters, allow_destroy: true, :reject_if => lambda { |a| a[:no].blank? }
    accepts_nested_attributes_for :paints, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }`
Run Code Online (Sandbox Code Playgroud)

app/models/shutter.rb A Shutter包含属于一个Job和一个Paint.

class Shutter < ActiveRecord::Base
    belongs_to :job
    belongs_to :paint
Run Code Online (Sandbox Code Playgroud)

app/models/paint.rb A Paint属于一个,JobShutters …

ruby sqlite ruby-on-rails ruby-on-rails-4 ruby-2.1

5
推荐指数
1
解决办法
1万
查看次数

如何在Python 3.2程序中优雅地包含Python 3.3 from None exception语法?

我正在尝试重新引发异常,以便为用户提供有关实际错误的更好信息.Python 3.3包括PEP 409.它添加了raise NewException from None语法来抑制原始异常的上下文.

但是,我的目标是Python 3.2.Python脚本将解析,但在运行时如果遇到from None它将产生的语法 TypeError: exception causes must derive from BaseException.例如:

try:
    regex_c = re.compile('^{}$'.format(regex)) 
except re.error as e:
    e_msg = 'Regular expression error in "{}"'.format(regex)
    e_reraise = Exception(e_msg)
    # Makes use of the new Python 3.3 exception syntax [from None]
    # to suppress the context of the original exception
    # Causes an additional TypeError exception in Python 3.2
    raise e_reraise from None
Run Code Online (Sandbox Code Playgroud)

封装raise e_reraise from None …

python exception forward-compatibility python-3.2 python-3.3

4
推荐指数
1
解决办法
1609
查看次数