SE250:lab-3:sbas046

From Marks Wiki
Revision as of 05:19, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (2 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Task 1

for n = 1000000
it takes 47 ticks

for n = 10000000
it takes 594 ticks

for n = 20000000
it takes 1140 ticks

for n = 100000000
it takes 6031 ticks

there is a roughly linear corrolation


Task 2

changing the "* 2" to "* 10" yields the following resluts

for n = 1000000
it takes 62 ticks

for n = 10000000
it takes 468 ticks

for n = 20000000
it takes 1062 ticks

for n = 100000000
it takes 4921 ticks

for n = 1000000000
it runs out of memory

This demonstrates that if we use a larger multiplier then as the array gets very large it is more efficent (ie: when we went to 100000000 repititions the program got considerably faster) but it also runs out of memory faster. The reason for this is that it is allocating considerably larger and larger chunks of memory each time therefor it does not need to reallocate memory as often, speeding up the process but it also reaches maximum memory much faster.


Task 3

for ARRAYLIST_MIN_ALLOC = 16000

The time speeds up slightly for smaller values but as we go higher the time taken is actually a little longer ( by about 50ticks)


Task 4

calling ensure_capacity speeds up the program very slightly, similar to increasing ARRAYLIST_MIN_ALLOC


Task 5

for n = 1000000
it takes 266 ticks

for n = 10000000
it takes 21563 ticks

This strategy is very fast for small arrays but as soon as we introduce larger arrays it becomes unsusable.


Task 6

Doing this takes forever! This is probably because the memory needs to be reallocated instead of just one character/integer being added to the end.

Sample Code

#include <stdio.h>
#include <time.h>

#include "arraylist.h"

int main (){

    long start, stop;
    ArrayList AL;
    int n, i;

    n = 10000;
    
    arraylist_init( &AL );

    start = clock();
	for (i=0; i<n; i++){
	    arraylist_put(&AL, 1, 0);
	}
	stop  = clock();
    
	printf("The time taken = %ld", (stop - start));

    return 0;
}