Go Down

Topic: Why is there a delay at the end of void Loop? (Read 4898 times) previous topic - next topic

Nick Gammon


There, now would you mind going back and reading what I actually wrote?


Well, I re-read it. Now instead of screaming at me if you would be kind enough to post an actual counter example I would be interested to see it. Code that compiles. Code that proves your point.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

gerg


edit: Hmm, so I skimmed a few replies above and see just the piggy-backing on Nick's reply and "using that reply to read my post", so let me make this much bigger for your reading ability:
I don't mean using goto in place of a loop is somehow going to magically save a bunch of code. Far the hell from it.


If in fact, you're saying that, in part, to my reply, as I think I'm reading it, I strongly encourage you to re-read what I said. Your tone implies you didn't understand what you read - or I expressed myself very poorly. I just re-read my post and to me, it hints of the former rather than the later. You also need to keep in mind, there is a highly diverse skill set present on these forums. Some started coding today. Some have decades of experience. Many times a post is used as a soap box to share a cautionary tale so as to ensure others are not unknowingly led astray; even with the best of intentions to the contrary.
http://maniacalbits.blogspot.com

Msquare

It is ... interesting(?) ... to notice that goto still has not lost it's magic - to get gentle and intelligent people into strong and heated debates.  :)
<rushes off before anyone can throw something>

Nick Gammon

You'll notice that I did not say to never use goto:


... it's simply a case of using the right tool for the job. The goto statement has its uses, in possibly 0.01% of cases.


If you look at Wikipedia:

http://en.wikipedia.org/wiki/Goto

To quote in part:

Quote
Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful.


Then:

Quote
Some programmers, such as Linux Kernel designer and coder Linus Torvalds or software engineer and book author Steve McConnell also object to Dijkstra's point of view, stating that GOTOs can be a useful language feature, improving program speed, size and code clearness, but only when used in a sensible way by a comparably sensible programmer.


So, respected programmers agree that goto is useful under certain conditions: "when used in a sensible way by a comparably sensible programmer". I would not disagree with that.

My test case was intended to illustrate that, using the case of the original post in this thread, that the goto achieved nothing that a while loop would not have achieved. That was my point. For that case, I correctly showed that the goto did not speed it up, it was omitting the function call/return sequence, which the while loop also did exactly as efficiently.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

gerg

To be fair, I believe he was referring to optimizations which are possible because of default flow-throughs and jumps, whereby its possible to avoid the expense of some comparisons. And/or a combination of jump tables. Obviously practical gains there would be CPU/architecture dependent based on the costs of jumps vs jump comparisons, branch prediction, and cache/pipeline availability. And in complex cases where multiple comparisons are required, its frequently possible to avoid multiple comparisons whereby progressive defaults can be assumed, saving some here and there. Typically a case a real rat's nest of logic is required and a goto can help alleviate, but even then, actual savings may not be there.

In the cases above which I eluded, they were typically some from column a and some from column b. Sometimes, adding a goto can actually improve readability when the conditionals are plentiful and complex. Providing for a small optimization and easing readability, IMOHO, can be a big win. I personally believe the vast, vast majority of those types of logical rat's nests stem from poor coding and/or poor design and thusly, the number of legitimate cases will be extremely rare and hard to come by unless you live 24/7 in hardcore optimization land. Very few coders do. Thusly my off the wall statement of likely applicability.
http://maniacalbits.blogspot.com

retrolefty

I think the vilified goto statement inherited most of it's bad reputation for it's use as a primary means of program flow control in the early versions of the BASIC language. A 'large' program written in early versions of BASIC (before they too added better structures) could be very difficult to follow and debug because of the need to trace through every goto command to tell what a program was actually doing.

Later better structured languages (including BASIC) showed far superior methods to break up large programs into functions, etc. So in my opinion the goto became more of a alias target for the legitimate criticism of the early BASIC programming language. While I have haven't yet found the need to use the goto in any of my arduino sketches, I'm certainly open minded enough to suspect that there could be valid uses of a goto in a C/C++ program.

Discussions of politics, religion, and the use of goto seems to always bring out the worst in people.  ;)

Lefty
   

Fletcher Chr

If speed is all there is to it .... why not overclock the chip by "upgradeing" the oscillator to a faster version?

-Fletcher

FalconFour


To be fair, I believe he was referring to optimizations which are possible because of default flow-throughs and jumps, whereby its possible to avoid the expense of some comparisons. And/or a combination of jump tables. Obviously practical gains there would be CPU/architecture dependent based on the costs of jumps vs jump comparisons, branch prediction, and cache/pipeline availability. And in complex cases where multiple comparisons are required, its frequently possible to avoid multiple comparisons whereby progressive defaults can be assumed, saving some here and there. Typically a case a real rat's nest of logic is required and a goto can help alleviate, but even then, actual savings may not be there.


Pretty much this. Sometimes, in certain logic constructs where the data doesn't very well self-modify to break/start a loop, for example, a "goto" not only makes the program more readable, but could be used to remove countless lines of nested loops and repetitive conditionals that would otherwise be necessary to mimic the useful "goto".

Personally, I haven't yet actually written "goto" into my programs. I only recently learned that it was even possible, by reading over a highly optimized compression algorithm (FastLZ) and seeing that it uses goto to break traditional C limitations and "conditionally jump" instead of "conditionally loop", which is the only construct that's really familiar to C coders. Sometimes, it may be necessary to conditionally jump to a previous part of the program, but that whole block would be pretty unreadable (or require unnecessary use of counter/status variables) if it were all placed into a "do { ... } while()" block.

That's what I meant about the use of goto. Not that it be used in place of loops at free will, but that it not entirely be *shunned*. That its use be frowned upon or admired *in its context*, not just the fact that the letters "goto" appear in someone's program. Sometimes it may be sloppy, but with limited use of "goto", I find it makes the code much easier to understand - I can just look for that label and pick it up right there, unconditionally, as I don't have to think about the setup and incrementing of a "for (x; y; z)" structure to see where variables will begin and end, what gets passed to it, etc.

And in terms of efficiency, a "goto" is one "jump" command in assembly. That's what I meant by being the most efficient branching method in C. No conditionals, just "jump". It's just as elegant as the command says it is :)

And finally, sorry if I was a bit abrasive in my reply there, but I really was upset when one guy mis-interpreted what I was trying to say (perhaps I didn't say it as clearly as I thought I did), then everyone kinda ran with it and gave me the stampede treatment. Kinda like a snowball of miscommunication landing on me.  XD

JBMetal

@everyone who mentioned it, I do use SPI for this

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

You've gone and translated my test code to:
Code: [Select]

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.

If you would like to argue the merits of goto vs while please start a new thread. Nobody said replacing while is slower than goto I believe? I may be incorrect on this as I only skimmed the heated responses ...

I would however like to know what options there are, in my test code example,  for me to branch code without using a function or goto as in the quote above?

AWOL

Quote
And the result: goto is faster than using a function call, end of story.

You missed the point too; there was no need for a goto.
"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.

JBMetal

@AWOL: You mean to say that if I don't need the 6KHz speed improvement I do not need to use goto, correct?
If you are hinting that the 6KHz improvement could be gained without using goto then please, you tell me with my code, how I can gain that 6KHz?

AWOL

#41
Jun 30, 2011, 08:24 pm Last Edit: Jun 30, 2011, 08:30 pm by AWOL Reason: 1
With the "while" loop - haven't you been following this?

It's been there since reply #5.

Do try to keep up.
"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.

draythomp

Oh, good grief.  Almost everyone involved in this discussion is right, and you're arguing about something that will never, ever be decided.

Just a touch of history.  The original languages used didn't have subroutine calls, the machines didn't have a stack, goto was all there was.  People HAD to code using goto.  As time went by, ideas happened and code and machines evolved.  I cut my teeth on an IBM360 and a DEC PDP11/45; one had a stack, the other didn't.  One had something spelled 'call' the other didn't.  Goto was common.

Code written with goto in it was often a problem because there was NOTHING else to help the programmer get done what needed to be done.  So, lousy programmers made a mess of things, just like today with obscure library code and indirect referrences.  Hence the rebellion against goto which software managers took up as an anathema and created 'coding standards' that made people avoid the use of what was once the ONLY flow control method available.  Tons of articles on 'structured programming' touted the 'new' paradigm and it almost became a religion.  Actually from the nature of the various responses, it may actually BE a religion.

Yes, I'm ignoring things like JNZ, jump not zero and JO, jump on overflow because these are just conditional goto statements.

Net, goto is just something that is not worth arguing about in the 21st century with all the other capabilities out there.  If a person wants to use goto, who cares, you don't have to use his code and convincing him what he's doing is somehow wrong isn't going to accomplish much.  Plus, you may be wrong, his method may be better somehow.

The first page of this thread was really useful.  I, for one, didn't know what the actual code was that called setup and loop; that was a valuable part of the discussion.  The timing of the return and call illustrated that hanging inside loop might be useful in time critical situations.  The argument over goto is something that many of us have heard a thousand times and just isn't interesting anymore.

Code it anyway you want to, just use reasonable names and comment the heck out of it.
Trying to keep my house under control http://www.desert-home.com/

JBMetal

@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?  

AWOL

What part of "while(1)" compiles into exactly the same code as your "goto" do you have difficulty understanding?

Quote
cut my teeth on an IBM360 and a DEC PDP11/45; one had a stack, the other didn't.

Even the lowly PDP8 had "JMS" (JuMp to Subroutine) - even if it was an awkward cuss to use.
"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.

Go Up