我正在尝试转换YCbCr-file 从8个基点到10个基点。在
到目前为止,我最好的方法还是慢一个数量级 而不是最基本的朴素C实现。在
C语言中的朴素方法,大约在8秒内运行 改为在大块上下功夫,把时间缩短到1秒以下
我很好奇它能得到什么样的表演 来自处理二进制文件的标准python。示例文件是 在CIF-resolution中,与1080p中的内容相比“小”。 尽管我主要感兴趣,也可以随意添加一些新的建议 在标准python中。在
测试文件可以从
http://trace.eas.asu.edu/yuv/foreman/foreman_cif.7z
正确的10位输出的sha1sum
是
Python: 在
#!/usr/bin/env python
import array
f_in = 'foreman_cif.yuv'
f_out = 'py_10bpp.yuv'
def bytesfromfile(f):
while True:
raw = array.array('B')
raw.fromstring(f.read(8192))
if not raw:
break
yield raw
with open(f_in, 'rb') as fd_in, \
open(f_out, 'wb') as fd_out:
for byte in bytesfromfile(fd_in):
data = []
for i in byte:
i <<= 2
data.append(i & 0xff)
data.append((i >> 8) & 0xff)
fd_out.write(array.array('B', data).tostring())
朴素的C-dito: 在
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int c;
int d[2];
FILE* fd_in;
FILE* fd_out;
fd_in = fopen("foreman_cif.yuv", "rb");
fd_out = fopen("c_10bpp.yuv", "wb");
while((c = fgetc(fd_in)) != EOF) {
c <<= 2;
d[0] = c & 0xff;
d[1] = (c >> 8) & 0xff;
fwrite(&d[0], 1, 1, fd_out);
fwrite(&d[1], 1, 1, fd_out);
}
fclose(fd_in);
fclose(fd_out);
return EXIT_SUCCESS;
}
问题中的代码在我的机器上需要
25
秒,numpy
0.37
秒:
^{pr2}$cython
0.20
秒:其中
bpp8to10.pyx
:纯CPython版本的主要加速是将代码从模块级移动到函数(
main()
)6.7
秒(2个cpu):pypy
1.6
秒:相关问题 更多 >
编程相关推荐