Re: Analyzing Alg. Runtime
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Mon, 29 Jan 2007 21:23:20 +0100
"Brad" <yoda_132002@xxxxxxxxxxx> writes:
//This is a homework assignment so I am not looking for the answer,
but possibly an explination of how to figure it out.
I am given the algorithm (on line 2 it is suppose to be "i (less than
or equal to) n")
GSSSum
1. sum = 0;
2. for (i = 0; i < n; ++i) {
3. prod = 1;
4. for (j = 0; j < i; ++j)
5. prod = prod * x;
6. sum = sum + prod; }
7. return sum;
(x is a given value and n is non-negative integer)
I am suppse to analyze the run time for each line and then Compute the
total run time.
This is what i came up with. Not sure how right it is:
1. 1
2. n+1
3. n
4.
5.
6.
7. 1
Could someone let me know if I'm on the right track for the ones I
filled in an give some hints for the others.
Well, you can start this way. I'd rather start from the inside out.
The inner loop, line 5 is executed i times.
The outer loop, lines 3-6 are executed n times, with i going from 0 to
n-1 (or n if you replace < by <= on line 2).
So the total number of times line 5 is executed is 0+1+2+...+(n-1) (or n).
this sum is n*(n-1)/2 = ½n²-½n
If you want to give the result line by line, we get (with < in line 2):
1. 1
2. n
3. n
4. ½n²-½n
5. ½n²-½n
6. n
7. 1
Now, the for lines you could consider you execute them n+1 times, but
they are composed really of several instructions.
for(i=0; 1
i<n; n+1
++i){ n
}
so they are executed n+1 times only partially. In general, we don't
enter in these details, only if you want to count the exact number of
microprocessor instructions or cycles. But then, compiler
optimizations and processor pipelines may change entirely the way how
the code is executed. For example, the compiler "unroll" the loop, so
doesn't have to test for i<n and jump so often. It could start by
testing whether i+8<n, and copy 8 times the body of the loop separated
by ++i (or just with literal offsets).
Like:
for(i=0;i+8<n;i+=8){
prod = 1; for (j = 0; j < i+0; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+1; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+2; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+3; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+4; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+5; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+6; ++j) prod = prod * x; sum += prod;
prod = 1; for (j = 0; j < i+7; ++j) prod = prod * x; sum += prod; }
switch (n-i){
case 7: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 6: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 5: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 4: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 3: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 2: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 1: prod = 1; for (j = 0; j < i; ++j) prod = prod * x; sum += prod; ++i;
case 0: }
(it might more probably do that for inner loops than outer loops, but
you get the idea).
So i would just count n for line 2.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
.
- Follow-Ups:
- Re: Analyzing Alg. Runtime
- From: Brad
- Re: Analyzing Alg. Runtime
- References:
- Analyzing Alg. Runtime
- From: Brad
- Analyzing Alg. Runtime
- Prev by Date: Re: IIS and programing in .NET
- Next by Date: Re: Graph reduction algorithm
- Previous by thread: Analyzing Alg. Runtime
- Next by thread: Re: Analyzing Alg. Runtime
- Index(es):
Relevant Pages
|