SE250:lab-3:apra102

From Marks Wiki
Jump to navigation Jump to search

Task 1

By seeing the time thing here it remind me of lab1. clock function. Task1 for this lab went so quick. I wrote a code to calculate time taken to insert n elements to an arraylist using the function ayyaylist_push.

 int main() 
{	  
          ArrayList as;

      	  ArrayList bs;
         
         int i;
  	  int n = 1000000000;

         clock_t t0; 
         arraylist_init(&as);
         t0 = clock();

         for (i=0; i<n; i++){
         void arraylist_push( ArrayList *alist, element_t elt );}	  
         printf("%d\n", clock()-t0);
		
         return 0;   }

When I compile this code i got the following as answers:

'n'                        Time(msec)
n=1000                     4msc    
n=10000                    0msc
n=1000000                  2474msc   
n=1000000000               3284msc

It came up with different answers for different values of n. The most strange thing is when i compiled the code except in the second case it makes scense in all other cases. Why is it showing 0 millisec for n=10000 when it is 4 millisec for n=1000.



Task 2

When I tried to change the capacity in the code from 2 the results are as follows:

 capacity                    Time(msec)
alist->capacity * 4.0         186
alist->capacity * 10          193  
alist->capacity * 100         279
alist->capacity * 1000        334

In this way a we increase the capacity time is gradually increasing.



Task 3

Now by changing the ARRAYLIST_MIN_ALLOC size, the time changes as follows:

    Size                    Time(msec)
ARRAYLIST_MIN_ALLOC 16         187
ARRAYLIST_MIN_ALLOC 50         178  
ARRAYLIST_MIN_ALLOC 100        180
ARRAYLIST_MIN_ALLOC 1000       170
ARRAYLIST_MIN_ALLOC 10000      203

In this way the time is fluctuating. And I observed that the fuction ARRAYLIST_MIN_ALLOC does'nt have munch impact on time. As there is not much change in time.



Task 4

In this task by allocating some memory before we push the elements in the array it does have impact on time as there is a remarkable change in time. It shows the time as follows.

ensure_capacity( &as, required_capacity )

By using this code it showed the time as 176milliseconds, as the time took before for the same thing is 234milliseconds. There is a change in time by allocating the space.



Task 5

When i replaced *2 with +1000 time incredbly increased where the time is 155 if it is times 2 but time became 203 when the 1000 is added. As I keep on adding '0' to the value time is decreasing.



Task 6

When I replaced arraylist_push to array_put. Time took to push the value is less than time took to put the value. May be because it is easy to add the value at the end of the array instead of adding in front of the array.

The difficuly part that faced in this lab is when i was trying to add elements in front that is Task 6.