Author: Uri Blass
Date: 13:20:04 07/08/03
Go up one level in this thread
On July 08, 2003 at 15:06:48, Andrew Dados wrote:
>On July 07, 2003 at 17:18:20, Uri Blass wrote:
>
>>On July 07, 2003 at 16:41:03, Russell Reagan wrote:
>>
>>>On July 07, 2003 at 16:21:45, Uri Blass wrote:
>>>
>>>>I almost do not use goto in my program and I learned that I should not do it.
>>>
>>>goto is not bad always. A long time ago people didn't know the correct way to
>>>use goto, and they used it in a lot of wrong ways, so everyone started saying,
>>>"don't use goto! goto is bad!" and that made people think that it was bad. It is
>>>only bad if you don't know when to use it. It is ok to use it to jump out of a
>>>nested loop, for instance.
>>>
>>>for (x = 0; x < 8; x++) {
>>> for (y = 0; y < 8; y++) {
>>> if (some_condition())
>>> goto done;
>>> }
>>>}
>>>done:
>>>
>>>In this case, you want to get out of BOTH loops, so you can't just use a
>>>'break'. You could create extra variables and add more if statements to
>>>accomplish the same thing, but a goto works best in this situation, and it's
>>>okay to use it here
>>
>>I think that my code do it without goto
>>In your example there are also simple alternatives without goto.
>>
>>for (x=0;x<8;x++){
>> for (y=0;y<8;y++){
>> if (some_condition())
>> {
>> x=8;
>> y=8;
>> }
>> }
>>}
>>
>>
>>Another possibility is to define xy=8*x+y and to do
>>xy=0;
>>while (xy<64)
>>{
>> x=xy>>3;
>> y=xy&7;
>> if somecondition()
>> xy=64;
>> else
>> xy++;
>>}
>>
>>Note that I added
>>x=xy>>3; and y=xy&7 only for simplicty and it may be faster not to have these
>>varaibles and to change the condition to calculate them based on x and y when
>>you need it.
>>
>>Uri
>
>Hello Uri. Everyone is entitled to his own oppinion and I think 'evil of goto'
>is another programming legend and on many occasions well used goto simplifies
>loop design. Of course if you use goto every 10 lines then you are writing in
>BASIC :)
>
> Now think about 'break' statement: it is 'labelless goto' in fact; I have
>trouble reading code full of breaks when I need to count brackets; if I see goto
>I see where it gets to clearly. Did you see any article about evils of break
>statement?
>
>-Andrew-
I do not know.
I also assumed that goto may be slower because the people who write the compiler
care more to optimize while loops when optimizing goto statement is not
important because almost nobody use them so if there is a simple way not to use
goto I always prefer it.
If you talk about trouble of reading then I assume that C was made for speed
even if you need to sacrifice some convenience of reading.
I decided to use your way without goto because if gives the same effect.
Tim's code does not use the information that the probability for bits is not the
same and other codes seem to be too complicated and when I ask about speed I
prefer to think about the algorithm and not about questions that are computer
dependent.
If there is something that help both the computer to be faster and me to be
faster I like it.
If there is something that help the computer to be faster but is not logical for
humans then I do not like it.
I do not think to try to optimize function like abs because there is no faster
way than simple_abs from human algorithm and I do not care about computer
algorithms.
practically I use max from math.h because I assume that everything there is
optimized for speed.
I did not like Tim's code because the while loop use
the same switch again and again and I feel that it lose time from human point of
view because if I need to do switch for 8 cases then it is slower than switch
for 7 cases and I want to do switch for 8 cases only in the first time and later
switch for less cases.
Omid's abs does not save time relative to simple abs from human point of view
and is more expensive for humans.
If I have known function that save computer time for abs then I am going to use
it but I do not like to spend time on finding these functions.
I may decide to save some of the codes that people gave me to try them later but
at this time the most important thing is not to have bugs because I did not
finish calculating the information that I plan to calculate.
Some explanation:
I add new arrays and it gives me an idea to change the way that I calculate
other arrays because at the time that I calculate the new array I get
information that can help me to calculate the old arrays and it seems stupid for
me not to use it.
At this time it is more important to write the change without bugs and not to
write the change in a way that the computer does it as fast as possible.
I want to earn speed but not by every possible mean.
Uri
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.