Pages: 1 2 3 [4]   Go Down
Author Topic: Why is there a delay at the end of void Loop?  (Read 4113 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@AWOL: No need to try and bait me. The question is what is faster, using goto or a function. You will note my test example included While (1) for both goto and function calls. Interpret the usability of the results as you will.
« Last Edit: June 30, 2011, 01:47:57 pm by JBMetal » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25708
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So what this whole thread boils down to is "do you imagine a function call, function return and unconditional branch can operate on the same sort of timescale as a single unconditional branch?"

I think that is what Americans call " a no brainer".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You said it, of course I am not convinced this was entirely apparent to the rest of the Arduino world .. including me.
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Even the lowly PDP8 had "JMS" (JuMp to Subroutine) - even if it was an awkward cuss to use.

Yes, but the IBM360, the flagship computer of the same era, didn't.
Logged

Trying to keep my house under control http://www.desert-home.com/

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25708
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Branch and link?
About the same as the PDP8.
Recreated on the PowerPC, IIRC.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

crap, you're right.  I forgot about that darn thing.  BAL put the return address in a register and the went to wherever, then you would BCR or something like it (I forget) to get back.  There were modes and code sections and all kinds of other things that drove programmers nuts trying to do sumthin.

The fortran and cobol compilers of the time sucked too.

We don't know how good we have it these days.
Logged

Trying to keep my house under control http://www.desert-home.com/

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick Gammon: Sir, you are confusing the readers of this thread, I believe, and drawing attention away from the results presented.

I'm sorry if I appear to be confusing people that wasn't the intention. You asked why using goto seemed to be faster. I explained, I hope, that the goto wasn't faster, it was avoiding the function return. I also tried to explain that the goto statement was not "single most absolutely efficient branching method possible" available to a programmer.

You've gone and translated my test code to:
Code:
start:
  LATCH_ON();
  LATCH_OFF();
  LATCH_ON();
  LATCH_OFF();
 goto start;

Then only stating on a single line -
Quote
The goto didn't save time. It was changing the code to omit the repeated function calls that saved time. But you can do that without using goto.

This was the whole point of the exercise! And the result: goto is faster than using a function call, end of story.

I haven't "gone and translated" anything. That was your test code in your first post.

Here:

Code:
void loop() {
  start:
  LATCH_ON();
  LATCH_OFF();
  LATCH_ON();
  LATCH_OFF();
 goto start;
} // with goto

Without getting bogged down in arguments about whether goto is useful in other places, my response was that you could have replaced it with this:

Code:
void loop() {
 while (1)
  {
  LATCH_ON();
  LATCH_OFF();
  LATCH_ON();
  LATCH_OFF();
  }
} // with goto

I was trying to make the point that goto did not speed things up any more than a while loop would have. Then the fur started flying, regrettably.


@AWOL: I do believe you misunderstand, the goto tests proved:
Goto is faster to use than a function while still maintaining the ability to reuse code.

Or .. do you maintain that no functions should be used at all and everything be rolled out in the main loop? 

No, the goto wasn't faster. The while was just as fast. It was avoiding the function call. Confusion or not, and despite all the abuse I am getting, I hope to at least make this point.

Function calls have an overhead. Loops have an overhead. It's a tradeoff. Your code (a few lines up) unrolled a loop (you have two latch-on/latch-offs). That would be slightly faster than putting one in a loop, like this:

Code:
while (1)
  {
  LATCH_ON();
  LATCH_OFF();
  }

And even if you replaced while by goto, you still get that overhead. That single machine instruction that branches back to the start of the loop.

In absolutely time-critical code you may need to unroll loops. You may need to avoid function calls. You may need to use goto. But someone who is doing that has probably got the manual out, working at assembly level, and is working out how many machine cycles each instruction takes.

And for what? The hours you spend doing this could be avoided by spending $10 on the next faster chip in the range.
Logged

Dallas, Texas
Offline Offline
Sr. Member
****
Karma: 3
Posts: 267
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Since this thread has been referred to as religious zeal, I'll run with that.

And for what? The hours you spend doing this could be avoided by spending $10 on the next faster chip in the range.

AAAaaaaaaaamen....

For the vast majority of people, just pretend goto doesn't exist. As Nick clearly showed, in this case (and in typical cases), goto brings nothing to the table traditional flow control mechanisms don't't already provide. More traditional flow contorl mechanisms are typically easier to read. So why complicate things or give the goto-police, fodder?

So sayeth the compiiiiiilllller. AAAAAaaaaaaaameeeennnn.....

 smiley-lol smiley-lol smiley-lol smiley-lol  smiley-eek  
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Nick Gammon: I do apologise, I realise now where this confusion comes in.

The 'faster' goto results I am referring to is from this post: http://arduino.cc/forum/index.php/topic,65014.msg474960.html#msg474960

The 'other' speed improvement you were referring to, from the very beginning of this post, is because an additional loop was placed inside the void Loop{}
The only reason I used goto there is because I browsed accross it in the reference and thought I would use it (nostalgic reasons I suppose). Turned out to be a very bad idea actually ..

To make matters clear, in my tests I learned 2 main things about gaining some speed in this post:
1. Put a while(1) loop inside void Loop{}.
2. Replacing  the most often used/speed sensitive function with an inline goto in the main loop.

The reasons for this was graciously explained by many contributors here, including you Nick and very eloquently by AWOL.

Again, my apologies Nick, it was I who, unwittingly,  created the two seperate issues.

Insofar the use of goto is concerned in general - we all create own rats nests and then have to lie in it.
 smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No worries, I'm glad it all got sorted out!
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: