SE250:lab-3:srag014
Task 1
#include <time.h> #include "arraylist.h" int main(){ double time_period; int x; ArrayList arr; clock_t begin; clock_t end; arraylist_init(&arr); begin = clock(); for (x = 0; x < 10000000; x++) { arraylist_push(&arr,4); } end = clock(); time_period = ((double) (end- begin)) / CLOCKS_PER_SEC; printf("Time taken to push 10000000 into the array took %lf seconds\n", time_period); }
The output of the code was:
Time taken to push 10000000 into the array took 1.548000 seconds
Task 2
The objective of this task is to change the growth factor to any thing other than the default value (default is *2) and to see how/if the results change.
When growth factor was increased to:
- 3, Time taken to push 10000000 into the array took 1.548000 seconds
- 5, Time taken to push 10000000 into the array took 1.580000 seconds
- 7, Time taken to push 10000000 into the array took 1.424000 seconds
- 1.20, Time taken to push 10000000 into the array took 2.093000 seconds
- 1.01, Progaram crashes!
From the results I got it seems as if there is only a marginal difference as the growth factor is increased.
As the growth factor nears 1 though the time taken to compute the programme increases, and at 1.01 growth factor the program crashes which is understandable as the number is too big for the memory capacity.
Task 3
Restoring the original growth factor (ie.2.0) and repeating the experiment with a variety of initial array sizes produces the following times:
- ARRAYLIST_MIN_ALLOC 1000000, Time taken to push 10000000 into the array took 1.570000 seconds
- ARRAYLIST_MIN_ALLOC 100000, Time taken to push 10000000 into the array took 1.498000 seconds
- ARRAYLIST_MIN_ALLOC 10000, Time taken to push 10000000 into the array took 1.474000 seconds
- ARRAYLIST_MIN_ALLOC 1000, Time taken to push 10000000 into the array took 1.511000 seconds
- ARRAYLIST_MIN_ALLOC 100, Time taken to push 10000000 into the array took 1.628000 seconds
- ARRAYLIST_MIN_ALLOC 10,Time taken to push 10000000 into the array took 1.827000 seconds
- ARRAYLIST_MIN_ALLOC 1,Time taken to push 10000000 into the array took 2.023000 seconds
From those results it seems the general trend is that time increases as ARRAYLIST_MIN_ALLOC is increased.
Task 4
By adding the line:
ensure_capacity(&arr,10000000 );
The output derived after this modification is:
Time taken to push 10000000 into the array took 1.355000 seconds
There is a marked time difference between the preallocated one and the other one.
Task 5
I tried this both with the ensure_capacity line of code and without it.
Whithout the ensure_capacity line of code, the program took ages to compute, so had to bring down the iterations to 1000000,the output I derived from this was :
Time taken to push 10000000 into the array took 3.219000 seconds
With the ensure_capacity line of code, the time I derived for 10000000 (default) was:
Time taken to push 10000000 into the array took 1.328 seconds
Task 6
In this task, the objective is to use array_put instead of array_push. When I used the array_put, my program compiles but it takes forever to display the output. So basically the lesson I think we were meant to get from this task is that adding elements to the back of the list is faster than adding elements to the front of the list.