내 생각 엔 뭔가 잘못하고있는 것 같습니다. 나는 libjpeg-turbo를 gdk.PixbufLoader와 비교했을 때 속도 차이가 거의 발견되지 않았다. 내가 사용한 코드는 다음과 같습니다. libjpeg 터보를 들어
(jpegload.c) : 파이썬 GDK를 들어
#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);
}
(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)
그리고 결과 : gi.repository를 사용
$ python gi_load.py DSC_8450.JPG
time spent in decompression: 74 msec
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
빨리도 gi.repository
를 사용하여 순수한 C 버전을 작동, 그래서 당신은 여전히 하나 뭔가 잘못하고, 또는 너무 많이 기대하고있다.
당신이 시간이 소비되는 위치를 확인할 수 파이썬 프로파일 링 도구를 사용하여 고려 유무 : 여기
소스입니까? –Pixbufloader 닫기 호출은 현재 내 런타임의 94 %이며, 그 호출 내에서 자세한 내용을 얻지는 못합니다. –