loop trick

Discussion in 'Programming & Software Development' started by Luke212, Jul 22, 2007.

  1. Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    heres a cool trick if you need to loop a very large collection:

    seems about 10-15% faster than checking the limit every iteration

    Code:
                int len = 300000000;
                ushort[] a = new ushort[len];
                
                
                try
                {
                    uint ac = 0;
                    while (true)
                    {
                        a[ac] = 100;
                        ac++;
    
                    }
                }
                catch (IndexOutOfRangeException e2)
                {
                }
    
    // wow that was fast
    
     
  2. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,790
    Location:
    Caulfield-ish
    please tell me im missing something.... your forcing it to bassically crash instead of doing proper bounds checking?
    I certainly hope we are never working on the same code.
     
  3. Tanus

    Tanus Member

    Joined:
    Jun 27, 2001
    Messages:
    923
    Location:
    Melbourne
    Yeah, because it's never possible that you might overflow into some memory that shouldn't be touched, right?
     
  4. houseofzeus

    houseofzeus Member

    Joined:
    Mar 25, 2005
    Messages:
    3,195
    Location:
    St. Lucia, Brisbane
    Seriously, don't you think everyone would be doing this if it wasn't a bad idea :p
     
  5. Ravenclaw

    Ravenclaw Member

    Joined:
    Dec 6, 2004
    Messages:
    2,090
    for most cases it will be slower. benchmark 1,000,000 loops of 300 elements
     
  6. yoink

    yoink Member

    Joined:
    Feb 19, 2002
    Messages:
    3,484
    I'm guessing that's C? In javascript, the "minus-minus" trick works the best:

    Code:
    for( var i = data.length - 1; i >= 0; i-- ) {
        tmp = data[i];
    };
    Assuming you have a large variable called data :)
     
  7. shift

    shift Member

    Joined:
    Jul 28, 2001
    Messages:
    2,942
    Location:
    Hillcrest, Logan
    It'd never work in straight C, there's no bounds checking.

    I would guess the OP is a Java "programmer".
     
  8. bugayev

    bugayev Whammy!

    Joined:
    May 15, 2003
    Messages:
    4,093
    Location:
    Melbourne
    From his other threads, I think he's actually a .NET programmer.
     
  9. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    its a valid trick. there is no danger because it is managed.

    incrementing from length to 0 with -- is also a speed increase as mentioned above.
     
  10. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    there is a point where it goes from being slower to faster. in your situation you can always try both and see.
     
  11. GregDude

    GregDude Member

    Joined:
    Aug 12, 2002
    Messages:
    1,304
    Location:
    Brisbane
    Argh! Close you eyes young programmers. Please never use exception handling as flow control.
     
  12. SLATYE

    SLATYE SLATYE, not SLAYTE

    Joined:
    Nov 11, 2002
    Messages:
    26,854
    Location:
    Canberra
    Indeed. As we were taught, exception handling is for just what the name suggests - exceptional circumstances. Not just where it makes things easy.

    This sort of approach was used in one of the finalists from the OMGWTF calculator contest. It scares me that people are suggesting it as a way of writing 'proper' code.
     
  13. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    All you provide is a claim from authority that you were 'told' not to use it. This is fine, but be sure to find the specific reasoning as well.

    You can write a whole event based program on exception handling. it will work every time. It is similar, in some ways, to the CALLBACK system used by windows XP (not sure Vista yet). but i wouldnt recommend THAT.

    :thumbup:
     
  14. bugayev

    bugayev Whammy!

    Joined:
    May 15, 2003
    Messages:
    4,093
    Location:
    Melbourne
    Callbacks are not similar to exceptions.

    An exception is raised when an error condition is encountered - and your program logic should either handle it, or die.

    A callback is raised when a given worker thread has completed its processing and is indicating that the next stage should commence. If an callback encounters an error, it will raise an Exception, as will its parent thread.

    Exceptions are stack-based whereas callbacks are effectively a "goto"
     
  15. yoink

    yoink Member

    Joined:
    Feb 19, 2002
    Messages:
    3,484
    I certainly agree with bugayev - don't use exception handling to check for the end of a loop, it is bad practice. It comes down to this: code readability vs performance - code readability should always win. :thumbup:
     
  16. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    i never said they were. callbacks are an events system. exceptions are mechanisms you can use to build an events system.
     
  17. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    its ok for a hobby project but not for a paid job. most people seeing that will go 'wtf'.

    hehe
     
  18. yoink

    yoink Member

    Joined:
    Feb 19, 2002
    Messages:
    3,484
    Well, if it's just a 'hobby' project, does it really need that performance boost? ;)
     
  19. checkers

    checkers Member

    Joined:
    Jan 22, 2007
    Messages:
    613
    Location:
    Perth
    Hear hear! Good practices exist and are recommended for a reason.
    There are some exceptions however, anything that is highly performance dependant - such as database sorting or 3d graphics apps - will usually benefit more from fast code than pretty code. That's definately the minority of programs though, of my code at least spends all its time waiting for user input :leet:
     
  20. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    so if you HAD to write a scientific app you wouldnt ever use this technique?

    i for one welcome our Exception Overlords.
     

Share This Page

Advertisement: