CS 301 - Homework 6

Here are a bunch of programs that are much slower than they could be.  Using the "Time" mode of NetRun, speed each one up by a factor of 5 or more.  Be sure to keep the output exactly the same.
  1. Speed this program up by eliminating the divide. Keep the loop in place. (Executable NetRun Link)
    int divby=128;
    int foo(void) {
    unsigned int i, sum=0, val=1024;
    for (i=0;i<val;i++) sum+=i/divby;
    return sum;
  2. Figure out why this is slow, and speed it up. You must leave the loop in place; but anything else is fair game. (Executable NetRun Link)
    int get_thingy(void); /* prototype for routine below */

    int foo(void) {
    int i, sum=0, val=1024;
    for (i=0;i<val;i++) sum+=get_thingy();
    return sum;

    int get_thingy(void) {
    return 2;
  3. Here's a C++ performance pitfall I've seen too many times. Don't mess with my_class.
    (Executable NetRun Link)
    class my_class {
    int val;
    my_class() {val=1;}
    char scratch_data[0x54321];

    int some_function(my_class c,int d) {
    return c.val+d;

    int foo(void) {
    my_class c;
    return some_function(c,0x1234+sizeof(my_class));
As usual, you'll turn these problem in by just naming them HW6_1, HW6_2, etc. in NetRun

Problems are due Monday, November 7, at Midnight. 

O. Lawlor, ffosl@uaf.edu
Up to: Class Site, CS, UAF