aboutsummaryrefslogtreecommitdiff
path: root/entries/margoseltzer/efficiency.c
blob: 6e017f3f84f9ec39f4cb6ad401eeaf6befaf47ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
unsigned long foo(unsigned long n) {
     if (n < 2) return (n);
     return(foo(n - 1) + foo(n - 2));
}

// Efficiency of expression

unsigned long bar(unsigned long n) {
    unsigned long i, last;
    unsigned long sum, tmp;

    if (n < 2) return (n);
    last = 0;
    sum = 1;
    for (i = 2; i <= n; i++) {
        tmp = sum;
        sum += last;
        last = tmp;
    }
    return (sum);
}

// Efficiency in resource utilization