Python和GObject的图像加载性能问题
我有一个用GTK(GObject)做界面的脚本,用来发布我的照片博客。
我想通过在后台线程加载图片来提高它的响应速度。
但是我尝试在后台线程中填充GdkPixbuf对象时一直没有成功,所有的尝试都卡住了。
所以我想了个替代办法,就是在后台线程中读取文件,然后根据需要把它们放入GdkPixbuf。这种方法的性能结果让我感到惊讶和沮丧,真让我怀疑我是不是做错了什么。
我在用的是从相机拍的轻微压缩的jpeg文件,大小大约在3.8MB左右。
这是原来的阻塞式图片加载:
pb = GdkPixbuf.Pixbuf.new_from_file(image_file)
这个平均大约需要550毫秒,虽然不算太长,但如果你想快速浏览十几张图片,这就显得有点繁琐了。
然后我把它拆分开了,先是文件读取:
data = bytearray(open(self.image_file).read())
这个平均只需要15毫秒,真不错,但也让我有点担心,如果读取文件只要15毫秒,那剩下的535毫秒都花在哪了呢?
顺便提一下,使用bytearray是因为PixBufLoader不接受其他格式的数据。
接下来是Pixbuf加载:
pbl = GdkPixbuf.PixbufLoader()
pbl.write(data, len(data))
pbl.close()
pb = pbl.get_pixbuf()
这个平均大约需要1400毫秒,几乎是让Gtk自己处理的三倍时间。
我是不是在这里做错了什么?
2 个回答
我用pygtk开发了一个小型的图片查看器。我使用了PixbufLoader,但每次写入时只输入N个字节。结合idle_add()这个功能,我可以在后台加载图片,同时应用程序仍然能响应用户的操作。
这里是源代码: http://guettli.sourceforge.net/gthumpy/src/ImageCache.py
我的猜测是:你可能做错了什么。我刚刚对比了libjpeg-turbo和gdk.PixbufLoader,发现它们的速度几乎没有差别。我用的代码如下。
对于libjpeg-turbo(jpegload.c):
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <jpeglib.h>
void decompress(FILE* fd)
{
JSAMPARRAY buffer;
int row_stride;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fd);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
}
int main(int argc, char** argv)
{
long len;
FILE *fd;
unsigned char *buf;
struct timeval start, end;
int i;
const int N = 100;
int delta;
/* read file to cache it in memory */
assert(argc == 2);
fd = fopen(argv[1], "rb");
fseek(fd, 0, SEEK_END);
len = ftell(fd);
rewind(fd);
buf = malloc(len);
assert(buf != NULL);
assert(fread(buf, 1, len, fd) == len);
gettimeofday(&start, NULL);
for(i = 0; i < N; i++) {
rewind(fd);
decompress(fd);
}
gettimeofday(&end, NULL);
if(end.tv_sec > start.tv_sec) {
delta = (end.tv_sec - start.tv_sec - 1) * 1000;
end.tv_usec += 1000000;
}
delta += (end.tv_usec - start.tv_usec) / 1000;
printf("time spent in decompression: %d msec\n",
delta/N);
}
对于python gdk(gdk_load.py):
import sys
import gtk
import time
def decompress(data):
pbl = gtk.gdk.PixbufLoader()
pbl.write(data, len(data))
pbl.close()
return pbl.get_pixbuf()
data = open(sys.argv[1]).read()
N = 100
start = time.time()
for i in xrange(N):
decompress(data)
end = time.time()
print "time spent in decompression: %d msec" % int((end - start) * 1000 / N)
测试运行结果:
$ gcc jpegload.c -ljpeg
$ ./a.out DSC_8450.JPG
time spent in decompression: 75 msec
$ python gdk_load.py DSC_8450.JPG
time spent in decompression: 75 msec
$ identify DSC_8450.JPG
DSC_8450.JPG JPEG 3008x2000 3008x2000+0+0 8-bit DirectClass 2.626MB 0.000u 0:00.019
补充:还有另一个测试,这次使用gi.repository
:
import sys
import time
from gi.repository import GdkPixbuf
def decompress(filename):
pb = GdkPixbuf.Pixbuf.new_from_file(filename)
return pb
N = 100
start = time.time()
for i in xrange(N):
decompress(sys.argv[1])
end = time.time()
print "time spent in decompression: %d msec" % int((end - start) * 1000 / N)
结果是:
$ python gi_load.py DSC_8450.JPG
time spent in decompression: 74 msec
使用gi.repository的GdkPixbuf.PixbufLoader确实比“纯粹的”gtk.gdk
慢得多,代码如下:
import sys
import time
from gi.repository import GdkPixbuf
def decompress(data):
pbl = GdkPixbuf.PixbufLoader()
pbl.write(data, len(data))
pbl.close()
return pbl.get_pixbuf()
data = bytearray(open(sys.argv[1]).read())
N = 100
start = time.time()
for i in xrange(N):
decompress(data)
end = time.time()
print "time spent in decompression: %d msec" % int((end - start) * 1000 / N)
结果:
$ python gi_load.py DSC_8450.JPG
time spent in decompression: 412 msec
但是GdkPixbuf.Pixbuf.new_from_file
的速度和纯C版本一样快,即使使用gi.repository
,所以你要么是做错了什么,要么是期望太高。