- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
这是对 this answer 的跟进我之前的问题 Fastest approach to read thousands of images into one big numpy array .
在 chapter 2.3 "Memory allocation of the ndarray" ,Travis Oliphant 写了以下关于如何在内存中访问 C 有序 numpy 数组的索引。
...to move through computer memory sequentially, the last index is incremented first, followed by the second-to-last index and so forth.
这可以通过沿两个第一个或两个最后一个索引对二维数组的访问时间进行基准测试来确认(出于我的目的,这是加载 500 个大小为 512x512 像素的图像的模拟):
import numpy as np
N = 512
n = 500
a = np.random.randint(0,255,(N,N))
def last_and_second_last():
'''Store along the two last indexes'''
imgs = np.empty((n,N,N), dtype='uint16')
for num in range(n):
imgs[num,:,:] = a
return imgs
def second_and_third_last():
'''Store along the two first indexes'''
imgs = np.empty((N,N,n), dtype='uint16')
for num in range(n):
imgs[:,:,num] = a
return imgs
基准测试
In [2]: %timeit last_and_second_last()
136 ms ± 2.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [3]: %timeit second_and_third_last()
1.56 s ± 10.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
到目前为止一切顺利。但是,当我沿最后一个维度和倒数第三个维度加载数组时,这几乎与将它们加载到最后两个维度一样快。
def last_and_third_last():
'''Store along the last and first indexes'''
imgs = np.empty((N,n,N), dtype='uint16')
for num in range(n):
imgs[:,num,:] = a
return imgs
基准测试
In [4]: %timeit last_and_third_last()
149 ms ± 227 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
last_and_third_last()
我的速度如此接近last_and_second_last()
相比 second_and third_last()
?最佳答案
我将尝试说明索引,而不涉及处理器缓存等细节。
让我们创建一个具有不同元素值的小型 3d 数组:
In [473]: X = np.mgrid[100:300:100,10:30:10,1:4:1].sum(axis=0)
In [474]: X
Out[474]:
array([[[111, 112, 113],
[121, 122, 123]],
[[211, 212, 213],
[221, 222, 223]]])
In [475]: X.shape
Out[475]: (2, 2, 3)
ravel
将其视为一维数组,并向我们展示值在内存中的布局方式。 (顺便说一下,这是默认的 C
排序)
In [476]: X.ravel()
Out[476]: array([111, 112, 113, 121, 122, 123, 211, 212, 213, 221, 222, 223])
当我在第一个维度上建立索引时,我得到 2*3 值,即上述列表的连续 block :
In [477]: X[0,:,:].ravel()
Out[477]: array([111, 112, 113, 121, 122, 123])
在最后一个索引上给出 4 个值,从整个数组中选择 - 我添加了 ..
以突出显示它
In [478]: X[:,:,0].ravel()
Out[478]: array([111,.. 121,.. 211,.. 221])
中间的索引给了我 2 个连续的子 block ,即 2 行 X
。
In [479]: X[:,0,:].ravel()
Out[479]: array([111, 112, 113,.. 211, 212, 213])
通过 strides
和 shape
计算 numpy
可以访问 X
中的任何一个元素(关于)同时。在 X[:,:,i]
的情况下,这就是它必须做的。这 4 个值“分散”在数据缓冲区中。
但如果它可以访问连续的 block ,例如在 X[i,:,:]
中,它可以将更多的操作委托(delegate)给低级编译和处理器代码。使用 X[:,i,:]
时,这些 block 没有那么大,但可能仍然大到足以产生重大影响。
在您的测试用例中,[n,:,:]
在 512*512 元素 block 上迭代 500 次。
[:,n,:]
必须将该访问分成 512 个 block ,每个 block 512 个。
[:,:,n]
必须进行 500 x 512 x 512 次单独的访问。
我想知道使用 uint16
是否会夸大效果。在另一个问题中,我们刚刚展示了使用 float16
的计算要慢得多(高达 10 倍),因为处理器(和编译器)被调整为使用 32 位和 64 位数字。如果处理器被调整为移动 64 位数字 block ,那么移动一个隔离的 16 位数字可能需要大量额外的处理。这就像从文档中逐字复制粘贴一样,而逐行复制每次复制所需的击键次数更少。
确切的细节隐藏在处理器、操作系统和编译器以及 numpy
代码中,但希望这能让您了解为什么您的中间情况更接近最优而不是最坏的情况。
在测试中 - 将 imgs
设置为 a.dtype
在所有情况下都会减慢速度。所以 'uint16' 不会引起任何特殊问题。
Why does `numpy.einsum` work faster with `float32` than `float16` or `uint16`?
关于python - 与倒数第二个相比,最后一个索引对 numpy 数组的访问时间的影响更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115571/
这是一个非常笼统的问题,我希望我能答对。 我正在研究 SSL/TLS 重新协商并已阅读了一些内容。这是我从阅读中了解到的内容: 从 SSL/TLS 重新协商的角度来看,客户端分为两个主要组,打补丁的和
第一个屏幕是艺术的细节。当我向上滚动时,标题将是 alpha。我点击另一个“艺术”到另一个细节 UI,然后按回到 Previous UI。之前的UI标题是黑色的,怎么变透明了。 布局:
想知道 mv 对基表的影响。它会减慢基表的速度吗?它什么时候开始写入 mv,就像同时写入基表和 mv 一样? 如果我有 local_quorum 的 CL 且 RF=3,客户端是否必须等到写入 mv
似乎在任何地方都找不到太多关于此问题的帮助,所以我想我会在这里尝试。 我正在尝试制作一个简单的 for 循环,当我将鼠标悬停在 html 卡上时,它会隐藏卡中的一些文本。该卡有一个简单的名字和姓氏,我
我有一个程序每帧运行 tick() 方法。我希望一个对象根据设定的重力常数下落,因此我创建了一个 Ball 对象,该对象会将其位置更新为前一帧的位置减去 y 速度。每个刻度 y 速度都会减少重力常数。
我的 KeyHandler 在这里: private void KeyHandler(java.awt.event.KeyEvent evt) {
我有一个方法,其中使用了很多其他类,包括链接列表、队列和堆栈。在我的方法中,我有一个 for 循环,我想在其中弹出堆栈(方便地命名为 s)并将队列(方便地命名为 q)出队到 s1 和 q1。由于某种原
我有一个 JTree 节点数组和另一个自定义对象的相应数组。 我想要什么:当选择 JTree 的节点时,相应对象(其数组中索引与节点数组中所选节点索引相同的对象)的字段填充 JLabels。 我被困在
我知道浏览器完成了处理客户端脚本(Javascript、JQuery 等)的所有工作,但想知道在性能方面是否还有其他重要因素(网络速度、客户端计算机速度、服务器环境) 如果它完全依赖于浏览器(类型和版
我有一个 Android 服务在后台运行,它将使用以下代码: while(true) { ServerSocket server = new ServerSocket(1234); Socke
对JQM有以下疑惑: 1.如果我们在单独的 html 文件中使用重复的 id,对 jquery mobile 有什么影响。 假设我们在单独的 html 文件中有重复的 id,但如果我们不使用该 id
我正在尝试更新两个(inventory、sold)MySQL 表的表库存。 假设我们正在处理的 sku 是 BT888-16 UPDATE inventory JOIN sold ON invento
我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变! public class user_AllBooks extends AbstractTableModel
我想在向表中插入数据时创建一个 MYSQL 存储过程,数据也会被插入到其他服务器表中。 我知道这在 ORACLE 数据库中是可能的,但我不知道它是否适用于 MYSQL。 有什么办法吗? 最佳答案 是的
我在 css 方面非常糟糕,只能靠 SO 答案来解决 - 但是我找不到针对这个特定问题的任何解释。 我有一个表单,其中包含一个 textarea 和一个 button(input/submit),仅此
我在一个元素上有动画,但它的移动也会影响 sibling 。如何在不影响兄弟元素的情况下仅在元素上使用动画? 问题示例: function animateSearch() { $('.glyph
我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显
自从我使用 JavaScript 以来已经有一段时间了 - 在获得证书之后我开始学习 Perl 并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可
我正在处理一个 HTML 元素,我添加了一个复选框,选中后会高亮显示所有文本输入字段。唯一的问题是一些输入字段在表格内,出于某种原因我无法用我的代码影响它们。任何帮助将不胜感激。 相关代码: HTML
我为 String 类创建了一个小扩展,以便方便地从中删除字符。这是它的样子: mutating func drop(characters chars: [String]) { for c i
我是一名优秀的程序员,十分优秀!