-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
90 lines (78 loc) · 2.23 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include IMPL
static double diff_in_second(struct timespec t1, struct timespec t2)
{
struct timespec diff;
if (t2.tv_nsec-t1.tv_nsec < 0) {
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
} else {
diff.tv_sec = t2.tv_sec - t1.tv_sec;
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec;
}
return (diff.tv_sec + diff.tv_nsec / 1000000000.0);
}
int main()
{
int number;
struct timespec start,end;
double cpu_time;
FILE *time_out;
FILE *result_out;
FILE *src_file = fopen("testcase.txt","r");
if( src_file == NULL ) {
printf("Can't open the file\n");
return -1;
}
fscanf(src_file,"%d",&number);
int* src1 = (int *)malloc(sizeof(int)*number);
if( src1 == NULL ) {
printf("Can't allocate memory\n");
return -1;
}
int* src2 = (int *)malloc(sizeof(int)*number);
if( src2 == NULL ) {
printf("Can't allocate memory\n");
return -1;
}
int* dst = (int *)malloc(sizeof(int)*number*2);
if( dst == NULL ) {
printf("Can't allocate memory\n");
return -1;
}
for(int i=0; i<number; i++)
fscanf(src_file,"%d",&src1[i]);
for(int i=0; i<number; i++)
fscanf(src_file,"%d",&src2[i]);
fclose(src_file);
#ifndef OPT
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
merge(src1,src2,dst,number);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
cpu_time = diff_in_second(start,end);
time_out=fopen("orig.txt","a");
fprintf(time_out,"%lf\n",cpu_time);
fclose(time_out);
printf("execution time: %lf\n",cpu_time);
result_out = fopen("ans_orig.txt","w");
#else
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
merge(src1,src2,dst,number);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
cpu_time = diff_in_second(start,end);
time_out=fopen("opt.txt","a");
fprintf(time_out,"%lf\n",cpu_time);
fclose(time_out);
printf("executon time: %lf\n",cpu_time);
result_out = fopen("ans_opt.txt","w");
#endif
number*=2;
for(int i=0;i<number;i++)
fprintf(result_out,"%d ",dst[i]);
free(src1);
free(src2);
free(dst);
return 0;
}