Mercurial > hg > Applications > Grep
comparison c/dfaTobin/main.cc @ 45:89a198fa6b23
add dfrTobin
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 18 May 2015 13:34:36 +0900 |
parents | |
children | e13aac1cff0f |
comparison
equal
deleted
inserted
replaced
44:62739627f8ec | 45:89a198fa6b23 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <unistd.h> | |
4 #include <fcntl.h> | |
5 #include <string.h> | |
6 #include <stdbool.h> | |
7 #include <sys/stat.h> | |
8 #include <sys/time.h> | |
9 #include <sys/types.h> | |
10 #include "read.h" | |
11 #include "test.h" | |
12 | |
13 static double st_time; | |
14 static double ed_time; | |
15 | |
16 /* | |
17 * -b : bulk read 一括read | |
18 * -d : divide read 分割read (default) | |
19 */ | |
20 const char *usr_help_str = "Usage : ./read_lseek [-file filename] [-b -d]"; | |
21 const int ONE_TASK_READ_SIZE = 4096 * 1; | |
22 bool bulk_flag = true; | |
23 bool divide_flag = false; | |
24 | |
25 void result_printf(int,char *); | |
26 void divide_read(int,char*,read_t); | |
27 void bulk_read(int,char*,read_t); | |
28 | |
29 static double | |
30 get_time(){ | |
31 struct timeval tv; | |
32 gettimeofday(&tv,NULL); | |
33 return tv.tv_sec + (double)tv.tv_usec*1e-6; | |
34 } | |
35 | |
36 int main(int argc, char *argv[]){ | |
37 | |
38 struct stat sb; | |
39 read_t r; | |
40 | |
41 char *filename = 0; | |
42 for (int i = 1; argv[i]; ++i) { | |
43 if (strcmp(argv[i], "-file") == 0){ | |
44 filename = argv[i+1]; | |
45 }else if(strcmp(argv[i], "-b") == 0){ | |
46 bulk_flag = true; | |
47 divide_flag = false; | |
48 }else if(strcmp(argv[i], "-d") == 0){ | |
49 bulk_flag = false; | |
50 divide_flag = true; | |
51 } | |
52 } | |
53 | |
54 if (filename == 0){ | |
55 puts(usr_help_str); | |
56 exit(1); | |
57 } | |
58 | |
59 st_time = get_time(); | |
60 | |
61 int fd = -1; | |
62 if ((fd=open(filename,O_RDONLY,0666))==0){ | |
63 fprintf(stderr,"can't open %s\n",filename); | |
64 } | |
65 | |
66 if (fstat(fd,&sb)){ | |
67 fprintf(stderr,"can't open %s\n",filename); | |
68 } | |
69 | |
70 r.text_size = sb.st_size; | |
71 char *text = (char *)malloc(sizeof(char)*r.text_size); | |
72 | |
73 r.one_task_read_size = ONE_TASK_READ_SIZE; | |
74 | |
75 r.task_num = r.text_size / r.one_task_read_size; | |
76 r.task_num += ((r.text_size % r.one_task_read_size) != 0); | |
77 | |
78 if(bulk_flag == true){ | |
79 bulk_read(fd,text,r); | |
80 }else if(divide_flag == true){ | |
81 divide_read(fd,text,r); | |
82 } | |
83 | |
84 free(text); | |
85 close(fd); | |
86 | |
87 ed_time = get_time(); | |
88 printf("Time: %0.6f\n",ed_time-st_time); | |
89 | |
90 return 0; | |
91 } | |
92 | |
93 /* lseekで読む場所を指定してあげないといけないと思ったが、 | |
94 * readで読み込むだけでも読み込む場所が変わってくれるらしい。 | |
95 */ | |
96 void divide_read(int _fd,char* _text,read_t _r){ | |
97 for(int i = 0; i < _r.task_num; i++){ | |
98 //lseek(_fd, i * _r.one_task_read_size,SEEK_SET); | |
99 read(_fd,_text,_r.one_task_read_size); | |
100 result_printf(i,_text); | |
101 } | |
102 puts("divide read"); | |
103 printf("text size :%lu\n",_r.text_size); | |
104 printf("one task size:%d\n",_r.one_task_read_size); | |
105 printf("task num :%d\n",_r.task_num); | |
106 } | |
107 | |
108 void bulk_read(int _fd,char* _text,read_t _r){ | |
109 int i = 0; | |
110 read(_fd,_text,_r.text_size); | |
111 result_printf(i,_text); | |
112 puts("bulk read"); | |
113 printf("text size :%lu\n",_r.text_size); | |
114 } | |
115 | |
116 void result_printf(int _i,char *_text){ | |
117 //printf("-------%d--------\n",_i + 1); | |
118 printf("[task No. %d]%s\n",_i,_text); | |
119 //printf("-----------------\n\n"); | |
120 } |