图像加载性能问题与python和gobject
我有一个带有GTK(GObject)界面的脚本,用于发布到我的照片博客。
我试图通过在后台线程中加载图像来提高响应速度。
我没有尝试从后台线程填充GdkPixbuf对象,我试过的所有东西都只是卡住了。
所以作为替代方案,我认为我会在后台线程中读取这些文件,然后将它们按需推送到GdkPixbuf中。 这种方法产生了令人惊讶和令人沮丧的性能结果,这让我怀疑自己是否在做一些严重错误的事情。
我正在用我的相机轻轻压缩jpegs,他们倾向于在3.8mb左右。
这是原始的阻止图片加载:
pb = GdkPixbuf.Pixbuf.new_from_file(image_file)
这个平均值大约为550毫秒,并不是很大,但如果你想要通过十几张图像轻松浏览,那就很乏味。
然后我把它分开,这里是这个文件:
data = bytearray(open(self.image_file).read())
这个平均值为15ms,这真的很不错,但也有点令人担忧,如果我们能够在15ms内读取文件,其他535ms所花的时间是多少?
顺便说一句,存在bytearray调用是因为PixBufLoader不会接受数据。
然后Pixbuf负载:
pbl = GdkPixbuf.PixbufLoader()
pbl.write(data, len(data))
pbl.close()
pb = pbl.get_pixbuf()
这个平均值约为1400毫秒,比让Gtk做这一切的时间长近3倍。
我在这里做错了什么?
我的猜测是:你做错了什么。 我刚刚将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 msecn",
         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.repostiroy : 
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
  但是,即使使用gi.repository , GdkPixbuf.Pixbuf.new_from_file运行速度与纯C版本一样快,因此您仍然在做错或者期望过高。 
我用pygtk开发了一个小图像查看器。 我使用PixbufLoader,但每次写入只输入N个字节()。 结合idle_add()我可以在后台加载图像,而应用程序仍然响应用户输入。
这里是来源:http://guettli.sourceforge.net/gthumpy/src/ImageCache.py
链接地址: http://www.djcxy.com/p/6463.html上一篇: image loading performance problems with python and gobject
