创建文件与删除文件的小例子

在单目录下创建和删除大量1字节的文件所需的时间进行度量,文件名以xNNNNNN的格式来创建。数量在1000-20000之间。创建时随机,删除时按照文件名升序。

暂时还没有想出来该如何保存随机数组中的内容,执行两次函数后,随机数组的内容就变了。只能简单的用difftime()来简略的度量时间.

  1 #include "head.h"
  2 void create (int a[],int num);
  3 void delete(int a[],int num);
  4 void Bubble_sort(int a[],int num);
  5 int k[20000]={0};
  6 int main(int argc,char *argv[])
  7 
  8 {
  9     if(argc!=3)
 10     {
 11 
 12         printf("usage %s  flag numer \n",argv[0]);
 13         exit(1);
 14     }
 15     int flag=atoi(argv[1]);
 16     int num=atoi(argv[2]);
 17     if(num>19000)
 18     num=num-1000;
 19     int *a=calloc(num,sizeof(int));
 20     //int *k=calloc(num,sizeof(int));
 21     int i=0;
 22     srand((unsigned int)time (NULL));
 23     time_t start ,end;
 24     double diff;
 25 /*    if(flag==1)    
 26     {
 27     for(i=0;i<num;i++)
 28     {
 29         a[i]=rand()%num+1000;
 30         printf("%d\n",a[i]);
 31     }    
 32         //time(&start);
 33         create(a,num);
 34         //time(&end);
 35     }
 36     int m=0;
 37     for(i=0;i<num;i++)
 38     {
 39         k[m++]=a[i++];
 40     }
 41      if(flag==0)
 42     {
 43         Bubble_sort(k,num);
 44          for(i=0;i<num;i++)      
 45                 printf("%d\n",a[i]);
 46         delete(k,num);    
 47 
 48     }
 49     free(a);*/
 50     for(i=0;i<num;i++)
 51     {
 52         a[i]=rand()%num+1000;
 53         printf("%d\n",a[i]);
 54     }    
 55         time(&start);
 56         create(a,num);
 57         time(&end);
 58         
 59         diff=difftime(end,start);
 60         printf("创建%d个文件,耗时:%.2f秒\n",num,diff);
 61         Bubble_sort(a,num);
 62         time(&start);
 63         delete(a,num);
 64         time(&end);
 65         diff=difftime(end,start);
 66         printf("删除%d个文件,耗时:%.2f秒\n",num,diff);
 67     
 68     return 0;
 69 }
 70 
 71 void create (int a[],int num)
 72 {
 73     int i=0;
 74     char str[50]="0";
 75     for(i=0;i<num;i++)
 76     {        
 77 
 78         snprintf(str,50,"dd if=/dev/zero of=x%06d bs=1 count=1\n",a[i]);
 79         system(str);
 80         /*char s[20]="x";
 81         char command[100]="dd if=/dev/zero of=";    
 82         char command1[20]=" bs=1k count=1";
 83         snprintf(str,10,"%06d\n",i);
 84         strcat(s,str);
 85         strcat(command,s);
 86         strcat(command,command1);
 87         system(command);*/
 88             
 89     }        
 90 }
 91 void delete(int a[],int num)
 92 {
 93     int i=0;
 94     char str[50]="0";
 95     for (i=0;i<num;i++)
 96     {
 97         snprintf(str,20 ,"rm -f x%06d ",a[i]);
 98         system(str);
 99     }
100 }
101 void Bubble_sort(int a[],int num)
102 {
103     int i=0,j=0,temp=0;
104     for (i=0;i<num-1;i++)//比较的趟数
105     {
106     for(j=0;j<num-i-1;j++)//一趟内比较的次数
107     {
108         if(a[j]>a[j+1])
109         {
110             temp=a[j];
111             a[j]=a[j+1];
112             a[j+1]=temp;
113         }
114     }
115     }
116 }

head.h

 1 #include <sys/uio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <unistd.h>
 5 #include <stdlib.h>
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <fcntl.h>
 9 #include <errno.h>
10 #include <setjmp.h>
11 #include <pwd.h>
12 #include <grp.h>
13 #include <limits.h>
14 #include <shadow.h>
15 #include <sys/fsuid.h>
16 #include<signal.h>
17 #include <sys/wait.h>
18 #include<sys/time.h>
19 #include <time.h>
20 #include <sys/ipc.h>
21 #include <sys/msg.h>
22 #include <sys/shm.h>
23 #include <pthread.h>
24 #include <stddef.h>
25 #include <netinet/in.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <strings.h>
29 #include <arpa/inet.h>
30 void strerr(const char *msg)
31 {
32     fprintf(stdout,"%s is :%s",msg,strerror(errno));
33     exit(1);
34 }
35 void err(const char *msg)
36 {
37     perror("msg ");
38     exit(1);
39 }

 

posted @ 2017-11-26 12:02  快第三个十年  阅读(215)  评论(0编辑  收藏  举报