CS 415, Section 001 | Sonoma State University | Fall, 2022 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
Download the file analysis.tgz, and decompress it with the line
tar -xvf analysis.tgz
Upon decompression, a new directory analysis should have been created, and this directory should contain TestAnalysis.cpp, a main program, and functions (run1.cpp) ... run5.cpp)). TestAnalysis.cpp allows you to conduct experiments for the subsequent exercises. To compile this program use the line:
make TestAnalysis
The program uses command-line input.
For example, to run the code for Exercise 1 with n=100, you would type
./testAnalysis 1 100
Note: In this lab, a variable NUMBER_REPETITIONS is specified to repeat a segment of code NUMBER_REPETITIONS2 times. This allows reasonable timings to be compared within the accuracy of the clock.
Run program testAnalysis.cpp
for Exercise 1
with n=100, following the instructions above:
./testAnalysis 1 100
Although timings will vary from machine to machine, `lThe output likely will look similar to the following:
Program to run and time loop-based code segments Running Exercise 1 with time iterations = 100 run repeated 8000 squared times result returned: 50 looping time: 0.12 method execution: 3.93 execution time: 3.82
Now review the code to answer the following:
n
at
100 in each case. To what extent does the "looping time" change
with each experiment?
#include "loop.h" int run1(int n) { int sum = 0; for (int i=0 ; i < n ; i+=2) sum++; return sum; }
w++
is largely equal to x = x + 1
, P is roughly equal to A + S]
Note: In your answer, be sure to give a separate time required for each line of the code. Likely, the amount of time for a line will involve some expression involving at least some of the variables n, A, B, S, M, and P.
run1
.
n
is large. That is, for
large n
, which terms for total time will have minimal
impact on the total time, and why?
run1
procedure, with n equal 100 and
400.
n = 100
and n = 400
in your expression for
Big-Ω, and divide.)
#include "loop.h" int run2(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < n ; j++) sum++; return sum; }
Repeat steps a-f from Exercise 1 for this code.
#include "loop.h" int run3(int n) { int sum = 0; for (int i=0 ; i < n ; i++) sum++; for (int j=0 ; j < n ; j++) sum++; return sum; }
Repeat steps a-f from Exercise 1 for this code.
#include "loop.h" int run4(int n) { int sum = 0; for (int i=0 ; i < n ; i++) for (int j=0 ; j < n * n ; j++) sum++; return sum; }
Repeat steps a-f from Exercise 1 for this code.
#include "loop.h" int run5(int n) { int sum = 0; for (int i=1 ; i <=n ; i = i * 2 ) sum++; return sum; }
Repeat steps a-f from Exercise 1 for this code.
created December 21, 2021 revised December-January 2021 revised Summer 2022 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|