Thread: Optimalise a piece of code

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    Optimalise a piece of code

    Hello,

    I have an exam this friday, not an C exam.
    But one of the questions on the exam example is:

    optimalise this piece of code. What do you guy think that
    the BEST optimization is?

    Code:
    void Increase(int ∗ list , unsigned int size, int extra) {
        int i; 
    
        for( i =0; i <size ; i++)
            size[i] ∗= 36; 
    
        for ( i =0; i < size ;i++)
            size[i] = size[i] ∗ extra + size[i] ∗ 5;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I would start with code that actually compiled.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    It isn't a programming exercise but trying to find the best optimization for this code and the reason wy.

    So I need to add the 2 loops into one loop, but I am pretty sure i need to do something more, but I can't find what. Can somebody please help?

    Thank you

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    I am sorry, I forgot to adapt the code, On the example there was a error.

    Code:
    void Increase(int ∗ list , unsigned int size, int extra) {
        int i; 
    
        for( i =0; i <size ; i++)
            list[i] ∗= 36; 
    
        for ( i =0; i < size ;i++)
            list[i] = list[i] ∗ extra + list[i] ∗ 5;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    74
    If you know you have to take one loop out, why don't you like, try it

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    I am sorry but i think i did not explained my question that good.
    The given code becomes :
    Code:
    void Increase(int ∗ list , unsigned int size, int extra) {
        int i; 
        for ( i =0; i < size ;i++){
            list[i] ∗= 36; 
            list[i] = list[i] ∗ extra + list[i] ∗ 5;
        }
    }
    BUT I think this is to easy to be an exam question(university). So I believe there must be something more that can be optimised.

    For example: ++i is faster then i++,
    the use of bitinstructions,
    lazy evaluation,
    loop unrolling, loop fusion,
    add switch statements that are most used to the front,
    etc,...

    But I cant figure out if I am missing something in the code above

    Sorry if my question is ambiguous.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    For example: ++i is faster then i++,
    No, at least in C...

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Well, my book says: i++ copys the existing value to a temporal object, raises the internal value with one and returns that temporal value. But ++i raises the value and returns a reference.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thescratchy
    Well, my book says: i++ copys the existing value to a temporal object, raises the internal value with one and returns that temporal value. But ++i raises the value and returns a reference.
    Conceptually, yes. But when used as a standalone expression, a C compiler can be reasonably expected to generate the same code either way. In C++ it may be the case that a compiler will not apply such an optimisation for class types because operator++ could be overloaded such that the two versions are expected to produce different net results even when used standalone.

    Quote Originally Posted by thescratchy
    BUT I think this is to easy to be an exam question(university).
    Maybe your lecturer thinks otherwise. After all, we can expect that questions will be of varying degrees of difficulty. Besides, you told us what the exam is not about, but did not tell us what the exam is about, i.e., what you are actually being taught and tested.

    Quote Originally Posted by thescratchy
    But I cant figure out if I am missing something in the code above
    There is the micro-optimisation, which might be implemented by an optimising compiler, that changes this:
    Code:
    list[i] = list[i] ∗ extra + list[i] ∗ 5;
    to:
    Code:
    list[i] = list[i] ∗ (extra + 5);
    which can then be simplified to:
    Code:
    list[i] *= (extra + 5);
    Observe that (extra + 5) is constant in the loop body. Observe also that in the previous line, list[i] is multiplied by another constant, 36. Therefore, the loop can be written as:
    Code:
    unsigned int i;
    extra = 36 * (extra + 5);
    for (i = 0; i < size; i++) {
        list[i] *= extra;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you for your clear answer. Don't know why I didn't see that myself.
    Really appreciate your effort!

    Thank you very much

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @laserlight: Great optimization for both size and speed.

    @thescratchy: Remember when optimizing what your are optimizing for, either speed or size. Most times you have to pick one over the other.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:28 PM
  3. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM