在我写的程序里面,有个窗口第一次弹出的时候特别慢。程序在第二次执行的时候,该窗口弹出会快很多的。这个问题困扰了我很久。后来我用
clock()函数分别测试各个语句的执行速度,发现是QDir里的entryList函数执行起来特别耗时。写了个测试程序如下:
#include <QtCore/QCoreApplication>
#include <QDir>
#include <time.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
volatile clock_t clock_start;
volatile clock_t clock_stop;
volatile clock_t t1,t2;
clock_start = clock();
QDir dir;
t1 = clock();
dir.setPath("/mnt/pic");
t2 = clock();
QStringList imgSupportFormat;
imgSupportFormat<<"*.bmp"<<"*.png"<<"*.jpg"<<"*.gif";
QStringList list;
list = dir.entryList(imgSupportFormat);
clock_stop = clock();
qDebug("Time taken is %f seconds\n",(double)(t1-clock_start)/CLOCKS_PER_SEC);
qDebug("Time taken is %f seconds\n",(double)(t2-t1)/CLOCKS_PER_SEC);
qDebug("Time taken is %f seconds\n",(double)(clock_stop-t2)/CLOCKS_PER_SEC);
return a.exec();
}
测试结果为:
Time taken is 0.000000 seconds
Time taken is 0.000000 seconds
Time taken is 3.640000 seconds
我用的是qtE4.50,跑在blakfin548的板子上,板子上装的是uc。
奇怪的是:只有程序第一次执行的时候,会耗这么多时间,以后在执行的话,会快很多的。
哪位大侠指点下啊?
已解决:追踪源码到src/corelib/io/qfsfileengine_iterator_unix.cpp
在函数hasNext中有一句:
long maxPathName = ::pathconf(QFile::encodeName(path()).data(), _PC_NAME_MAX);
发现第一次执行此句时候会耗时达到3.64s。
debug出maxPathName文件名最大长度为260,直接修改 maxPathName = 260。问题解决
还是不知道为什么,执行long maxPathName = ::pathconf(QFile::encodeName(path()).data(), _PC_NAME_MAX);
会消耗那么多时间
[ 此帖被skysquall在2009-09-11 10:46重新编辑 ]