SE250:lab-3:tlou006
Q1
Using the code
int main() { int i,j,t0; ArrayList alist; arraylist_init( &alist ); for( j=0; j<10; j++ ) { t0 = clock(); for( i=0; i<1000000; i++ ) { arraylist_push( &alist, 0 ); } printf("time taken : %d\n", clock() - t0); arraylist_clear( &alist ); } arraylist_clear( &alist ); return 0; }
will hopefully give me the time taken to insert the number 0 one million times into the array list.
The loop is repeated 10times to give me an average.
I made the following change to the code
arraylist_push( &alist, i );
this should add a different number one million times into the array list.
My results showed that, apart from random errors, there is no difference in timing between adding the
same number and adding different numbers to the array list.
Q2
First of all I changed the array growth factor from 2.0 to a number lower than 1; eg 0.9
The result was an instant crash of the program. As expected ^___^
Changing the growth factor to 3.0 saw an increase in performance by about 3-4 milliseconds, this must
be because the function to expand the array had to be called less. I expected this performance boost to
increase as the growth factor increased until the calculation was almost instant, when the array only needed
to be expanded once due to the large growth factor.
Q3
I added
printf("time taken : %d for j = %d \n size is %d \n", j, clock() - t0, alist.capacity);
Hopefully this will help me spot the time changes.
Setting ARRAYLIST_MIN_ALLOC to 10000000 (a lot of memory so the array never runs out of space).
The performance was slower by about ~30milliseconds for a loop putting a number into the array one
million times. This was unexpected. I thought it would be slightly faster, as the array would never run
out of space and the growth calculations won't be needed each time.
Maybe it took time to allocate such a large amount of memory to something.
Q4
Added the line
ensure_capacity( &alist, 10000000 );
at the start of the code.
The time now was the same as the previous sections, ie. before ARRAYLIST_MIN_ALLOC was set to 10000000
faster by ~30milliseconds.
Ran out of time :( spent too long reading the code trying to understand
will finish later