-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgin2.c
73 lines (65 loc) · 1.49 KB
/
orgin2.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
#include <string.h>
#include <time.h>
#include <stdio.h>
//#define size 10
static long int diff_in_us(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 * 1000000000 + diff.tv_nsec );
}
void F(int* A,int* B,int N){
int temp = 1,zero_count=0;
for(int i=0;i<N;i++){
B[i] = 0;
//zero_count+= !A[i];
if(A[i])
temp *= A[i];
else
zero_count++;
}
switch(zero_count) {
case 0:
for(int i = 0;i<N;i++)
B[i] = temp/A[i];
break;
case 1:
for(int i = 0;i<N;i++){
if(A[i] == 0){
B[i]=temp;
break;
}
}
break;
default:
break;
}
return ;
}
int testNumber[7]={10,100,1000,10000,100000,1000000,10000000};
int A[10000000]={0},B[10000000]={0};
int main(int argc, char const *argv[])
{
FILE *fp = fopen("time3.txt","w");
struct timespec start, end;
for(int counter = 0;counter < 7;counter++){
#if defined(__GNUC__)
__builtin___clear_cache((char *) A, (char *) A + 8*testNumber[counter]*4);
#endif
for(int i = 0;i<testNumber[counter];i++){
A[i]=(i&3)+1;
}
clock_gettime(CLOCK_REALTIME, &start);
F(A,B,testNumber[counter]);
clock_gettime(CLOCK_REALTIME, &end);
fprintf(fp,"%d %ld\n",testNumber[counter], diff_in_us(start, end));
}
fclose(fp);
return 0;
}