Mongoose House

          Technical Edition

          統計

          留言簿(4)

          積分與排名

          閱讀排行榜

          一段測試DiskIO的代碼


          可以使用C標準庫(Windows-Based & Unix-Based兩方)的time函數取得精度為秒的時間,在Unix-Based系統中,使用gettimeofday函數取得更小精度的時間。

           1 #include <stdio.h>
           2 #include <stdlib.h>
           3 #include <time.h>
           4 #include <sys/time.h>
           5 int main(int argc, char **argv)
           6 {
           7     const char A = 'a';
           8     FILE *fp     = NULL;
           9     char * p     = NULL;
          10     long i       = 0L;
          11     long size    = 0L;
          12     time_t start = 0L;
          13     time_t end   = 0L;
          14     struct timeval tv_start = {0};
          15     struct timeval tv_end   = {0};    
          16     p = *++argv;
          17     if (p == NULL) {
          18         printf("Input file size by mb, please. SIZE(mb) = ");
          19         scanf("%d"&size);
          20     }
          21     if (size == 0L) {
          22         size = atoi(p);
          23     }
          24     fp = fopen("dump.dat""w");
          25     
          26     if(fp == NULL) 
          27     {
          28         perror("failed to open dump.dat");
          29         return EXIT_FAILURE;
          30     }
          31     timerclear(&tv_start);
          32     timerclear(&tv_end);
          33     /// time(&start);
          34     gettimeofday(&tv_start, NULL);
          35     for (i = 0; i < 1024 * 1024 * size; i++)
          36     {
          37         fputc(A, fp);
          38     }
          39     gettimeofday(&tv_end, NULL);
          40     /// time(&end);
          41     
          42     start = tv_start.tv_sec * 1000000 + tv_start.tv_usec;
          43     end = tv_end.tv_sec * 1000000 + tv_end.tv_usec;
          44     printf("Wrote %d bytes. fclose(fp) %s.\n", i, fclose(fp) == 0 ? "succeeded" : "failed");
          45     printf("TIME = %d.%3d s.\n", (end - start)/1000000, ((end - start)/1000)%1000);
          46     return EXIT_SUCCESS;
          47 }
          48 

          posted on 2011-03-26 20:16 Mongoose 閱讀(214) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 香格里拉县| 乌鲁木齐市| 光山县| 元朗区| 眉山市| 庆阳市| 集贤县| 安溪县| 林口县| 石台县| 泊头市| 陇南市| 汉源县| 北碚区| 南丹县| 渑池县| 南城县| 乌鲁木齐县| 汕头市| 泗阳县| 呈贡县| 利川市| 巴马| 洛阳市| 乌苏市| 北碚区| 岐山县| 肥西县| 孟津县| 金塔县| 历史| 油尖旺区| 舟山市| 阿巴嘎旗| 义马市| 吉安市| 得荣县| 红安县| 溧水县| 锡林浩特市| 岳西县|