Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

All those if statements inside loops... would be a lot cleaner (and faster!) to just use a single goto statement:

  goto cont;

  for (; ; ++z)
    for (; x <= z; ++x)
      for (; y <= z; ++y) {
        if (x*x + y*y == z*z) return *this;
  cont:
      }


I totally agree--thank you for bringing this up. I'm not sure it would be faster, but it is an interesting case for goto.

I saw another proposal referencing the original article which used a trio of functions in a nice way to achieve something similar without the goto (and without the ifs). My main point was to illustrate the use of custom iterators.


Consider me nerd-sniped. I typed the last post on my phone, but just had to bust out the laptop to check. Noticed a couple bugs I had: the for-loop initializers need to be set, and there needs to be an empty statement after the cont: label.

The goto version only has one unconditional branch outside of a loop, rather than three conditional branches inside loops. Anyways, the speed difference shouldn't be too big, as the branches are pretty predictable (pretty much always not taken).

Quick benchmark of the original and mine, up to the first 3000 triples:

  $ c++ -O3 pyth1.cpp  && time ./a.out | md5
  33aa33d6cad59951489757e06aeb5a15
  
  real    0m5.492s
  user    0m5.484s
  sys     0m0.007s
  $ c++ -O3 pyth2.cpp  && time ./a.out | md5
  33aa33d6cad59951489757e06aeb5a15
  
  real    0m4.248s
  user    0m4.238s
  sys     0m0.011s


I'm always looking for a legitimate excuse to use a goto in my C++ code because I'm perverse. It's very rare to find one but this looks like it might be okay.


All the crazy crap they added to C/C++, how was named loops and corresponding named break/continue not one of them?


In this case, the goto is into the loop, not out of it, so named break/continue would not help.

And the only point of having named break/continue would be to not use goto. I don't see the point in that, since to me, this:

    for (i = 0; i < ie; i++) {
        continue_i:
        for (j = 0; j < je; j++) {
            if (cond1)
                goto break_i;
            if (cond2)
                goto continue_i;
            /* ... */
        }
    }
    break_i:
is as readable as this:

    loop_i:
    for (i = 0; i < ie; i++) {
        for (j = 0; j < je; j++) {
            if (cond1)
                break loop_i;
            if (cond2)
                continue loop_i;
            /* ... */
        }
    }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: