loop trick

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

  1. xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    It doesn't matter how you dress it up it's still broken. Infact, it's worse because now you explicitly differentiate between real and false exceptions. Woe unto ye poor soul that tries to reuse this code.

    Either use the tool properly or find something more suitable that doesn't hinder your work.
     
  2. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    i think this thread has run its course :)
     
  3. bugayev

    bugayev Whammy!

    Joined:
    May 15, 2003
    Messages:
    4,093
    Location:
    Melbourne
    Why, because everyone thinks you're shit?

    I think this has been a really fascinating discussion on different implementations to a common solution and how (in some ways) *not* to solve a problem.

    I'd be disappointed to see it closed.
     
  4. seamer

    seamer Member

    Joined:
    Aug 19, 2001
    Messages:
    1,825
    Location:
    san jose, california
    I just tool around with piddly little projects, but even I know when you're using an interpreter you can only go as fast as the interpreter is capable of. Why use an intermediary when you can hit the direct route and just write in assembly?
     
  5. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    one not so obvious reason being because you cant write assembly as well or as fast as the run-time compiler will do for you AUTOMATICALLY.
     
  6. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    yeah its been fun, as long as everyone gets something out of it.

    but im running out of clever ideas.
     
  7. seamer

    seamer Member

    Joined:
    Aug 19, 2001
    Messages:
    1,825
    Location:
    san jose, california
    Getting a bit defensive there, lad. You're getting all kinds of free advise here from people who know what theyre doing (not me, I'm just a copy/paste/edit hack) and dismissing their experience because it doesnt mesh with yours.

    What will you do when your exception-throwing throws an exception you arent prepared for?
     
  8. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    not defensive, just witty. dont you get defensive :p i dont have to worry if it throws an exception that im not prepared for, because ill only catch the ones I want. everything else goes through the normal channels.
     
  9. shift

    shift Member

    Joined:
    Jul 28, 2001
    Messages:
    2,942
    Location:
    Hillcrest, Logan
    If you have a reasonable amount of assembly and processor internals knowledge, and you task is as simple as the examples you've given, you can easily outdo the compiler and run-time.
     
    Last edited: Jul 24, 2007
  10. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    my code is as far as you will get in managed code. if you want to spent the time writing assembly that is your perogative. The team leader probably wont authorise it any way.
     
  11. shift

    shift Member

    Joined:
    Jul 28, 2001
    Messages:
    2,942
    Location:
    Hillcrest, Logan
    So you imagine a project where it's necessary to abuse exception handling to save a few cycles per loop, but you can't do real optimizations that could make real differences?
     
  12. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    Like i said its a trick.

    You say 'abuse' like the code has feelings. Language is a tool we should make the most of it.
     
  13. mordy

    mordy Member

    Joined:
    Aug 30, 2001
    Messages:
    5,100
    Location:
    melb
    I have a new trick for u

    Coders dont optimise their code at all anymore. CPUs are really fast and programs are getting more complex. The best way to speed up ur program is to figure out elegant solutions to complex problems.

    The stuff your doing is done automatically if its needed.
     
  14. ACodingFettish

    ACodingFettish Member

    Joined:
    Feb 7, 2004
    Messages:
    6,652
    Location:
    Brisbane
    what, so because technology is advancing we should make dumber programs?
     
  15. mordy

    mordy Member

    Joined:
    Aug 30, 2001
    Messages:
    5,100
    Location:
    melb
    um no, because cpus have billions of cycles a second we dont have to optimise our add from 3 cycles to 2 using low-level assembly hacks.

    If the programming language says
    "var = a * b;"
    you can assume that however that happens is gonna be fast enough and thats the best you need to do.

    That said, there are times when you do need to optimise, a good example is the executable for running games, its usually optimised in assembly, and also when you use collections using efficient ADTs can create serious speed increases.

    Unrolling your loop manually or disabling bounds checking is not such a useful optimisation.
     
  16. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    some people cant or arent allowed to program low level, so they have to use the designated language. if this is the case, and it often is for me, then you need to know a few of these things in a pinch.
     
  17. 0oBelialo0

    0oBelialo0 Member

    Joined:
    Aug 12, 2002
    Messages:
    4,025
    Location:
    Canberra
    What a bizzarely interesting thread :)

    Want to know something horrible? I recently saw this as a recommended way to improve pl/sql loop performance. Can't find it now, but I think it may have come from Oracle...

    mordy has a good point - most developers these days aren't writing unoptimised code because they're lazy, they're doing it because there's more business benefit in solving a new problem. Of course there are exceptions (no pun...), but it's not often I've seen us need to low-level tune a .NET app.

    Oracle on the other hand... if I have to replace one more 'not in' with 'minus' I might just kill someone.

    Ben
     
  18. Ze.

    Ze. Member

    Joined:
    Sep 13, 2003
    Messages:
    7,871
    Location:
    Newcastle, NSW
    Why would the speed of CPU matter when it comes to optimisations? If we've got a CPU bound problem where we need the speed , surely we would want to optimise it?

    I guarantee that I can name a heap of problems where we'll always need more cpu speed.

    Some optimisations such as loop unrolling can be harmful sometimes such as when you transition from code that fits entirely within the cache to code that causes cache misses.

    luke212 : any speed advantage you've obtained depends entirely on the runtime environment you are testing it in , it's a silly trick anyway since a good compiler should recognise what you are doing and make the checks the same anyway.
     
    Last edited: Jul 25, 2007
  19. OP
    OP
    Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,041
    Location:
    Sydney
    well no, im finding differences in speed (C#) so i thought others might as well.
     
  20. akashra

    akashra Member

    Joined:
    Apr 25, 2003
    Messages:
    3,910
    Location:
    Melbourne, AU
    Absolutely.

    The people who have criticised you as not being a very good programmer here clearly aren't very good programmers themselves.

    As you've said, there's no danger (for C# or Java in this example).

    Regarding loop speed, 0 > value is quicker than value < variable.

    One of the tests we give new job candidates looks a bit like this:

    Code:
    int main(String [] args)
    {
     String s;
     BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
     try
     {
      while (true)
      {
       s += br.readLine().trim().toString() + "\n";
      }
     }
     catch (NullPointerException e)
     {
    
     }
    }
    There's dozens of things any decent java programmer should pick up on, not least of which being the use of StringBuilder for linear rather than quadratic performance.
     

Share This Page

Advertisement: