- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想要一个 buffer
的版本指向 bytearray
并且是可变的。我想将它传递给像 io.BufferedIOBase.readinto()
这样的 I/O 函数没有循环中的内存分配开销。
import sys, struct
ba = bytearray(2000)
lenbuf = bytearray(8)
with open(sys.argv[1]) as fp:
while True:
fp.readinto(lenbuf) # efficient version of fp.read(8)
dat_len = struct.unpack("Q", lenbuf)
buf = buffer(ba, 0, dat_len)
fp.readinto(buf) # efficient version of fp.read(dat_len), but
# yields TypeError: must be read-write buffer, not buffer
my_parse(buf)
我也试过 buf =
memoryview
(buffer(ba, 0, length))
但得到(本质上)相同的错误。
我认为使用 Python 不应该等同于不关注运行时性能。
我默认使用安装在 Cent6 上的 Python 2.6,但如果确实需要,可以切换到 2.7 或 3.x。
谢谢!
我对切片到 bytearray
的行为感到困惑。下面的记录表明我可以简单地从 bytearray
中取出一个切片:
>>> x = bytearray(10**8)
>>> cProfile.run('x[10:13]="abc"')
2 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
>>> x.count(b'\x00')
3999999997
>>> len(x)
4000000000
>>> cProfile.run('x[10:13]="abcd"') # intentionally try an inefficient case
2 function calls in 0.750 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.750 0.750 0.750 0.750 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
>>> len(x)
4000000001
但是,“可变切片”在分配单个字节时无法按预期工作:
>>> x = bytearray(4*10**9)
>>> x = bytearray(10)
>>> x[2] = 0xff
>>> x.count(b'\x00')
9
>>> x[3:5][0] = 0xff
>>> x.count(b'\x00')
9 # WHAT
我不会在我的应用程序中真正使用单字节赋值,但我担心是否存在任何根本性的误解。
最佳答案
您可以让它读取多余的数据,然后在从文件读取更多数据之前简单地使用字节数组中的所有多余数据。
否则你可以使用 numpy:
import sys, struct
import numpy as np
buf = np.zeros(2000, dtype=np.uint8)
lenbuf = bytearray(8)
with open(sys.argv[1]) as fp:
while True:
fp.readinto(lenbuf)
dat_len = struct.unpack("Q", lenbuf)
fp.readinto(buf[:dat_len])
my_parse(buf[:dat_len])
numpy 创建您需要的读写缓冲区,索引 [:dat_len] 返回数据子集的“ View ”而不是副本。由于 numpy 数组符合缓冲区协议(protocol),您可以进一步将它们与 struct.unpack() 一起使用,就好像它们是字节数组/缓冲区一样。
关于Python:如何获得指向字节数组的可变切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35403929/
有人能给我解释一下我下面程序中的一点吗我编写的以下程序是为了理解 int * const * var 的含义; #include "iostream" using namespace std ; in
我正在摆弄 C 中的指针,但仍然不确定一些非常基础的知识。我想出了以下示例代码: #include int main(void) { int num = 42; /
以下代码产生警告: const char * mystr = "\r\nHello"; void send_str(char * str); void main(void){ send_str
我正在尝试在我的 Linux 计算机上升级 cmake。这是我使用的命令。 删除旧的 cmake: sudo apt purge --auto-remove cmake 从 https://cmake
我试图借助 char 指针来追踪 union 第一个字节的值。我假设 byte1 的输出应该是 ff,但实际上是 ffffffff。如果我错了请纠正我。 #include #include #in
我正在使用微 Controller 进行一些 ADC 测量。当我尝试使用 -O2 优化编译以下代码时遇到问题,当代码中存在 PrintVal() 函数时,MCU 卡住。我做了一些调试,结果发现,当我添
#include #include using namespace std; int main () { vector qwerty; qwerty.push_back(5);
我有我的 woking setup.py 文件;其中包含以下行: home = os.environ["HOME"] # home = /home/dr in my machine; distu
这段代码 #include #include static_assert(std::is_same_v::value_type, volatile int>); 在最新的 GCC 和 clang
我对 C 中的前向声明有疑问。 代码 typedef struct yhash_s t_yhash;// forward declaration struct yhash_s { size_t
我想提取成员指针指向的成员的类型。 template void demo(myClass& instance, void* ptr) { instance.*member = *reinter
我正在尝试使用指针将一段 C 代码转换为 Swift 3。这是 C 代码中的相关部分。 Float32 sampleArray[256] = { // Array is 256 Float value
您可能知道,VLA's haves pros and cons 和它们在 C11 中是可选的。 我想使 VLA 成为可选项的主要原因是:“堆栈可能会爆炸”: int arr[n]; /* where
这段代码有什么错误?为什么我不能按照我尝试的方式取消引用该元素。 #include typedef struct { int value; struct node * left;
有什么方法可以在 JavaScript 中创建\返回指向变量的指针吗? 比如,在 PHP 中: function func() { ..... return &$result; } 我
如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样好 : type TAcceptor = function(filename:string):boolean of objec
很简单的问题: 我对 C++ 中的智能指针有点陌生。我想我得到了所有权的东西,但我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是得到了 unique_ptr 类的函数,这
我得到了一个点的方位 Angular 、指南针方向和一个可以将箭头设置到某个方向的 api(0 是顶部,90 是右侧,180 是底部,360 是顶部) 如果我希望箭头指向我采用方位 Angular 形
我正在尝试找到一种方法,从单元格中获取位于当前工作表左侧(托盘下方)的工作表单元格中的数据。 我知道如何通过调用其他工作表 =Sheet1!A1 但现在我需要一些最好的解释 =Sheet[-1]!A1
所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的
我是一名优秀的程序员,十分优秀!