SE250:lab-3:mabd065
Jump to navigation
Jump to search
|
Lab 3
Task 1
running the following code multiple times:
int main () { double start,end,diff; int i; ArrayList myarr; arraylist_init(&myarr); start = clock(); for (i = 0; i < 10000000; i++) { arraylist_push(&myarr, 5); } end = clock(); diff = end - start; printf("It took %.f ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Gives the following values:
It took 562 ticks / 1000 s. It took 579 ticks / 1000 s. It took 547 ticks / 1000 s.
Average: 563 ticks / 1000 s.
Task 2
CODE:
int main () { double start,end,diff; int i; ArrayList myarr; arraylist_init(&myarr); start = clock(); for (i = 0; i < 1000000000; i++) { arraylist_push(&myarr, 5); } end = clock(); diff = end - start; printf("It took %.f ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Results:
Put = 6.0 || It took 5031 ticks / 1000 s || 100 000 000 loops Put = 5.0 || It took 5078 ticks / 1000 s || 100 000 000 loops Put = 4.0 || ERROR || 100 000 000 loops || assertion "alist->arr != (element_t*)0" failed: file "arraylist.c", line 40 Put = 3.0 || ERROR || 100 000 000 loops || assertion "alist->arr != (element_t*)0" failed: file "arraylist.c", line 40 Put = 2.0 || It took 6093 ticks / 1000 s || 100 000 000 loops Put = 1.5 || It took 6875 ticks / 1000 s || 100 000 000 loops Put = 1.1 || It took 13796 ticks / 1000 s || 100 000 000 loops
Put = 1.001 || ERROR || 1 000 000 000 loops || 49 [main] lab3 2588 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION 13051 [main] lab3 2588 open_stackdumpfile: Dumping stack trace to lab3.exe.stackdump
As the factor increases, the number of reallocations decreases, hence the faster it takes to executs.
Task 3
CODE:
int main () { double start,end,diff; int i; ArrayList myarr; arraylist_init(&myarr); start = clock(); for (i = 0; i < 1000000000; i++) { arraylist_push(&myarr, 5); } end = clock(); diff = end - start; printf("It took %.f ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Results:
MIN Allocation = 16 || It took 6734 ticks / 1000 s || 100 000 000 loops MIN Allocation = 20 || ERROR || 100 000 000 loops MIN Allocation = 20 || It took 562 ticks / 1000 s || 1 0 000 000 loops MIN Allocation = 30 || It took 609 ticks / 1000 s || 1 0 000 000 loops MIN Allocation = 32 || It took 625 ticks / 1000 s || 1 0 000 000 loops MIN Allocation = 40 || It took 703 ticks / 1000 s || 1 0 000 000 loops
When MIN Allocation = 100 000 000: I get : assertion "alist->arr != (element_t*)0" failed: file "arraylist.c", line 40
because the MIN value is greater than the number of loops (10 000 000).
Task 4
CODE:
int main () { double start,end,diff; int i, number = 10000000; ArrayList myarr; arraylist_init(&myarr); start = clock(); ensure_capacity(&myarr, number); for (i = 0; i < number; i++) { arraylist_push(&myarr, 5); } end = clock(); diff = end - start; printf("It took %.f ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Results:
It took 485 ticks / 1000 s. It took 500 ticks / 1000 s. It took 484 ticks / 1000 s.
Average: 490 ticks / 1000 s.
This code runs faster (490) compared to the one in task 1 (563).
Task 5
CODE:
int main () { long start,end,diff; int i; ArrayList myarr; arraylist_init(&myarr); start = clock(); for (i = 0; i < 100000; i++) { arraylist_push(&myarr, 5); } end = clock(); diff = end - start; printf("It took %ld ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Results:
Loops = 100 000 || It took 0 ticks / 1000 s Loops = 100 000 || It took 15 ticks / 1000 s Loops = 100 000 || It took 15 ticks / 1000 s
Task 6
CODE :
int main () { long start,end,diff; int i; ArrayList myarr; arraylist_init(&myarr); start = clock(); for (i = 0; i < 10000; i++) { arraylist_put(&myarr, 5, 0); } end = clock(); diff = end - start; printf("It took %ld ticks / %d s.\n", diff , CLOCKS_PER_SEC ); }
Results:
Loops = 1 000 || It took 0 ticks / 1000 s Loops = 1 0 000 || It took 47 ticks / 1000 s Loops = 100 000 || It took 4625 ticks / 1000 s