-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchars_counter_seq.c
49 lines (38 loc) · 1008 Bytes
/
chars_counter_seq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main(){
struct timeval tstart, tend;
double exectime;
char *buf;
int len;
int length;
FILE *f = fopen("./lorem.txt", "r");
int ascii[128]={0};
if (f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(length);
if (buf)
{
fread(buf, 1, length, f);
}
fclose(f);
}
gettimeofday( &tstart, NULL );
for(int i=0;i<length;i++){
ascii[(int)buf[i]]++;
}
gettimeofday( &tend, NULL );
for(int i=0;i<128;i++){
printf("Occurrences of Ascii %d = %d\n",i,ascii[i]);
}
exectime = (tend.tv_sec - tstart.tv_sec) * 1000.0; // sec to ms
exectime += (tend.tv_usec - tstart.tv_usec) / 1000.0; // us to ms
printf("File Length: %d\n",length);
printf( "Execution time:%.3lf sec\n", exectime/1000.0);
return 0;
}