SE250:lab-3:vpup001
Lab 3 is focusing on arrays. I have read through the 'arraylist.c' file provided and familiarized myself with it. It took a lot of time to do that. I made sure that I understand what 'arraylist_init', 'arraylist_clear', 'arraylist_size', 'ensure_capacity', 'arraylist_put', 'arraylist_push', 'arraylist_get' and 'arraylist_pop' functions do.
1) The time taken to insert n elements using arraylist_push function is calculated.
int main(){ /*initialising the array*/ ArrayList xs; ArrayList ys; /*initialising the clock*/ clock_t t; int i; int n = 100000000; /*Passing in the address of the variable xs*/ arraylist_init( &xs ); /*calling the clock function*/ t = clock(); for (i=0; i<n; i++){ arraylist_push( &xs, 1 ); } printf("%d\n" , clock()-t); return 0; }
We can change the data of type i and n to long or something else. As we increase the size of 'n' the time taken increases but if we have 'n' as 1000000000 or larger the program crashes.
2) Changing the value of growth factor form 2 to some other number did not affect the performance but if the number is quite large. i.e, 50 or more, the runtime got slower and it took a lot of to insert elements into array (the value of t got larger). WHY?
3) There is no significant changes in the time if the value of ARRAYLIST_MIN_ALLOC is changed slightly. I have tried changing the value up to 10,000 and still there is not no significant difference to the time. But, the time has actually decreased if the value has increased to a very large number like 1000000 or larger. When the value of ARRAYLIST_MIN_ALLOC is increased the time taken to insert n = 1000000 elements has decreased.
4) In this task, we call the ensure_capacity function to pre-allocate some memory. In task 1, when the program did not have enough memory it allocates itself some memory form the 'arraylist_put' function. So, it takes time to go through the function.
But here calling ensure_capacity decreases the time.
ensure_capacity( &xs, n );
The program has enough space since ensure-capacity = n and the code runs really fast.
5) The growth strategy is changed to an incremental one. something like '+1000' If the value of growth strategy increases, the time taken decreases.
6) In this task we add elements to the front of the array list. so we called arraylist_put function with index 0. The program only runs with smaller n values. The run time is very slow if we have large n values and the time is larger too.