SE250:lab-3:gfun006

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

Task1

#include <time.h>
#include "arraylist.h"

double time_elapsed;
int i; 

int main()

{

ArrayList myArray; 

clock_t starttiming;
clock_t endtiming;

arraylist_init(&myArray);

starttiming = clock();

for (i = 0; i < 10000000; i++) {
 	 arraylist_push(&myArray, 5);
}


endtiming = clock();

time_elapsed = ((double) (endtiming - starttiming)) / CLOCKS_PER_SEC;
printf("Time taken using arraylist_push %lf seconds\n", time_elapsed);
printf("%d", arraylist_get(&myArray, 0));


return 0;

}

This is what I've done so far, and I have absolutely no idea if it is right or not. After some trouble figuring out how C works with the header files and calling functions and all that, I finally got to the code above. Here are a few results by varying the value of the element.

Time taken using arraylist_push 0.394000 seconds (value 5)
Time taken using arraylist_push 0.429000 seconds (value 5000)
Time taken using arraylist_push 0.393000 seconds (value 5000000)
Time taken using arraylist_push 0.481000 seconds (value 5000000000)

As you can see, it seems that varying the value does not affect the time taken that much at all. Interestingly enough.

Task 2

By changing the capacity in the code:

(int)(alist->capacity * 2.0)

When the capacity is changed to 1 or a very larger number (i.e. 6000), an error occurs in the compiler and terminates abruptly. In the case of changing the number to 1, it must crash because the reason capacity is multiplied by 2, is to make space when length equals capacity. If there capacity is not being increased, and elements are still being forced in, it probably led to it terminating. The reason for it terminating for a really big number is perhaps because the number is TOO big, and the computer memory exploded.

Task 3

By changing the value of ARRAYLIST_MIN_ALLOC 16, I got the following results (The value is still 5):

Time taken using arraylist_push 0.353000 seconds (ARRAYLIST_MIN_ALLOC 100) 
Time taken using arraylist_push 0.353000 seconds (ARRAYLIST_MIN_ALLOC 100000)
Time taken using arraylist_push 0.275000 seconds (ARRAYLIST_MIN_ALLOC 100000000)
Time taken using arraylist_push 0.270000 seconds (ARRAYLIST_MIN_ALLOC 100000000000)

Varying ARRAYLIST_MIN_ALLOC extremely does not seem to change the time that much. Perhaps the times are slightly decreasing because with the ridiculous amount of minimal allocations set up, there is less time wasted for increasing capacity all the time. But I suppose the time decreases are VERY small and could even probably be ignored.

Task 4

I added the ensure_capacity function to my code and added like the following:

arraylist_init(&myArray);
ensure_capacity(&myArray, 1)
starttiming = clock();

for (i = 0; i < 10000000; i++) {
	 arraylist_push(&myArray, 5);
}

Now, the compiler should know in advance that I will only be adding 1 element to the array each time. Here were the results I got (using the identical values I used in Task 1):

Time taken using arraylist_push 0.268000 seconds (value 5)
Time taken using arraylist_push 0.371000 seconds (value 5000)
Time taken using arraylist_push 0.405000 seconds (value 5000000)
Time taken using arraylist_push 0.423000 seconds (value 5000000000)

By comparing these results with task 1, it shows these times are faster. This makes sense because if the compiler already knew what the capacity was in advance, it would be much more efficient.

Task 5

By changing the capacity in the following code again:

(int)(alist->capacity * 2.0)

I got the following results:

Time taken using arraylist_push 0.347000 seconds (capacity 100000000, value 5)
Time taken using arraylist_push 0.599000 seconds (capacity 1000000, value 5)
Time taken using arraylist_push 30.633000 seconds (capacity 10000, value 5)

The smaller the capacity, the longer time it takes. This makes sense because my loop is very big (i.e. I am trying to calculate 10000000 times). Every time the capacity is filled up, it needs to increase. Therefore since I am looping it 10000000 times, if I only increase the capacity by 10000 each time, there is far not enough. Therefore it will keep on adding 10000 until it is finally enough, thus taking forever (as you can see it took 30.63300 for capacity 10000, compared to 0.347000 for 100000000)

Task 6

I replaced the arraylist_push function to arraylist_put, like this:

arraylist_init(&myArray);

starttiming = clock();

for (i = 0; i < 10000000; i++) {
 	 arraylist_put(&myArray, 5, 0);
}

If i ran that code, it would take be forever to get an output. Whereas with arraylist_push it only took 0.394000 to get an results. I think this may be because arraylist_push just puts an value at the end of the array and this can be very done and fast. While arraylist_put at index 0, it has to put the value and then push everything to the end of the array AND increase the capacity if needed, taking much longer. I concluded for this task if I was to get an output, I had to decrease my loop down to 100000, which produces the result:

Time taken using arraylist_push 5.053000 seconds