利用gettimeofday(),其精度为us级
#include <QDebug> #include <sys/time.h> struct timeval tStart; struct timeval tEnd; float timeUse; gettimeofday(&tStart, NULL); function(); gettimeofday(&tEnd, NULL); timeUse = (1000000*(tEnd.tv_sec - tStart.tv_sec) + tEnd.tv_usec - tStart.tv_usec)/1000000.0; qDebug() << timeUse << "s";