以下代码用于测试我系统上 libjpeg-turbo 的性能
unsigned char *source;
unsigned int _jpegSize = 0;
unsigned long long GetTime( void )
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
int main(int argc,char**argv)
{
if(argc < 3)
return 0;
char *fileName = argv[1];
int fileSize = atoi(argv[2]);
source = (unsigned char*)malloc(fileSize);
ReadFile(argv[1],fileSize); //Reads the file and copy the data to source buffer
for(int i=0; i<500; i++)
{
unsigned char* _compressedImage = source;
int jpegSubsamp, width, height;
tjhandle _jpegDecompressor = tjInitDecompress();
tjDecompressHeader2(_jpegDecompressor, _compressedImage, fileSize, &width, &height, &jpegSubsamp);
unsigned char dstBuffer[width*height*3]; //!< will contain the decompressed image
unsigned long long t1 = GetTime();
tjDecompress2(_jpegDecompressor, _compressedImage, fileSize, dstBuffer, width, 0/*pitch*/, height, TJPF_RGB, TJFLAG_FASTDCT);
unsigned long long t2 = GetTime();
printf("TimeTaken: %llu microseconds for %f ms\n", t2-t1,(float)((t2-t1)/(1000)));
tjDestroy(_jpegDecompressor);
usleep(50000);
}
它在 for 循环中重复解码 jpeg 图像。在我的系统上, tjDecompress2需要大约5-7 毫秒来解码 1024x768 图像。问题是,如果我 在循环末尾添加一个usleep(50000)命令,则tjDecompress2的执行时间会增加到25-28ms。这种行为的原因是什么以及如何解决。基本上我想以 20 FPS 从 ma USB 相机读取 MJPEG 图像并使用 libjpeg-turbo 对其进行解码,因此添加usleep(5000)的目的是模拟两个连续帧到达之间的时间