forked from xharaken/step2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_read.c
More file actions
executable file
·59 lines (47 loc) · 932 Bytes
/
memory_read.c
File metadata and controls
executable file
·59 lines (47 loc) · 932 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
double get_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
double random_01()
{
return (double)rand() / RAND_MAX;
}
int random_int(int from, int to)
{
return from + (int64_t)(random_01() * (to - from));
}
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s N\n", argv[0]);
return -1;
}
int n = atoi(argv[1]);
int* a = (int*)malloc(n * sizeof(int));
int* b = (int*)malloc(n * sizeof(int));
int i;
for (i = 0; i < n; i++) {
a[i] = i;
}
for (i = 0; i < n; i++) {
//b[i] = 0;
//b[i] = i;
b[i] = random_int(0, n);
}
double begin = get_time();
int sum = 0;
for (i = 0; i < n; i++) {
sum += a[b[i]];
}
double end = get_time();
printf("time: %.6lf sec\n", end - begin);
printf("sum: %d\n", sum);
free(a);
free(b);
return 0;
}