- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/time.h>
- float spend_time(const char *dirname);
- int main(int argc, char **argv)
- {
- int i;
- float time_count = 0;
- for(i = 0; i < 10000; i++) {
- time_count = spend_time(argv[1]);
- }
- fprintf(stdout, "Spend time is %8f\\n", time_count);
- return 0;
- }
- float spend_time(const char *dirname)
- {
- DIR * dir;
- struct dirent *dirp;
- struct timeval tpstart, tpend;
- float timeuse;
- gettimeofday(&tpstart, NULL);
- if((dir = opendir(dirname)) == NULL) {
- fprintf(stderr, "Open dir failed: %s\\n", strerror(errno));
- exit(0);
- }
- while((dirp = readdir(dir)) != NULL)
- ;
- closedir(dir);
- gettimeofday(&tpend, NULL);
- timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec -tpstart.tv_usec;
- return timeuse/1000000;
- }
- //该片段来自于http://www.codesnippet.cn/detail/131120137108.html
来源: http://www.codesnippet.cn/detail/131120137108.html