forked from osu-cs261-f23/recitation-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_dynarray_insert.c
52 lines (44 loc) · 1.17 KB
/
time_dynarray_insert.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
/*
* This file contains a program that calculates how long it takes to insert
* values into a dynamic array.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include "dynarray.h"
int main(int argc, char** argv) {
struct dynarray* da = dynarray_create();
int* values;
clock_t t0, t1;
int i, n;
/*
* Test dynamic array insertion timing for dataset sizes increasing from
* 10,000 to 1,000,000 in steps of 10,000.
*/
for (n = 10000; n <= 1000000; n += 10000) {
printf("%d\t", n);
fflush(stdout);
/*
* Allocate an array to store the dataset, and fill it with random numbers.
*/
values = malloc(n * sizeof(int));
assert(values);
for (i = 0; i < n; i++) {
values[i] = rand();
}
da = dynarray_create();
/*
* Insert all dataset values into the dynamic array, and compute the time
* it takes to perform all of the insertions.
*/
t0 = clock();
for (i = 0; i <n; i++) {
dynarray_insert(da, values + i); /* "values + i" == "&values[i]" */
}
t1 = clock();
printf("%f\n", (t1 - t0) / (float)CLOCKS_PER_SEC);
dynarray_free(da);
free(values);
}
}