C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 12-09-2002, 03:12 AM   #16
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Why the heck do you keep saying THE flow chart. What the heck are you talking about?
Polymorphic OOP is offline  
Old 12-09-2002, 03:15 AM   #17
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
sorry for the error, the code should be this ...
Code:
int i, contains_value = 0;
int error = 0;

for(i = 0; i < sizeof(array) && error == 0 ; i++)
    if(array[i] == target_value) {
        contains_value = 1;
        error = 1;
    }
beely is offline  
Old 12-09-2002, 03:20 AM   #18
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
I was going to make a joke about the flow chart thing but you replied too fast

Anyways, the two samples of code are functionally identical and both fairly optimized. But many times I don't happen to have the variable error handy. I never add variable when I don't need to. Our code is identical, only mine uses one less variable.
master5001 is offline  
Old 12-09-2002, 03:22 AM   #19
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Why? because your version introduces an entire new variable that has to be compared every single time it loops. A break handles the situation fine and doesn't mess with the flow in the manner that a goto does. Your version requires more space on the stack and adds a comparison on every iteration. Using a break doesn't introduce anything else.
Polymorphic OOP is offline  
Old 12-09-2002, 03:23 AM   #20
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
hmm, a bit question,
is the program better refer what's shown on flowchart ?
if it's true, then goto, continue, and break is not recommended replace at the certain program.

master5001, any comment on flowchart question ?
beely is offline  
Old 12-09-2002, 03:24 AM   #21
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
so how's the "break" statement on the flowchart look like ??
beely is offline  
Old 12-09-2002, 03:27 AM   #22
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
Quote:
Originally posted by master5001
I was going to make a joke about the flow chart thing but you replied too fast

Anyways, the two samples of code are functionally identical and both fairly optimized. But many times I don't happen to have the variable error handy. I never add variable when I don't need to. Our code is identical, only mine uses one less variable.
did you read a book called "C how to program" by deitel...

so, their comment said that goto, break, and continue, most of programmer didn't use them ..... bla bla bla ....

as a programmer, should they follow on the flowchart???
beely is offline  
Old 12-09-2002, 03:44 AM   #23
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Hmmm, now here is something that is very interesting. I wrote a quick program that tested the two loops it turns out that mine (the one with a break statement) was better optimized by the compiler since it took 9 seconds to go through an array 16777215 times whereas, yours took 27 seconds. That was with full optimizations turned on.
master5001 is offline  
Old 12-09-2002, 03:47 AM   #24
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Of course it took longer! Without the break he added an entire extra check on every single loop!
Polymorphic OOP is offline  
Old 12-09-2002, 03:52 AM   #25
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
so ?? without "break" . it's also can use other way. for eg:

for (i = 0 ; i<= MAX && end== 0 ; i++)
{
...
...
if (...){
..
..
end = 1;
}

so....?
beely is offline  
Old 12-09-2002, 03:58 AM   #26
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Well beely, I tried code that looked something like:

Code:
int i, contains_value = 0;
int error = 0;
size_t size;

for(size = sizeof(array);size-- && !contains_value;)
    if(array[array_size] == target_value)
        contains_value = 1;
And it inlined it (of course to keep things even I inlined mine as well) and the results are 15 secons for your method and 7 for mine. Sorry, but break, continue, and goto have their place in this universe. The bottom line is that your compiler understands that it needs to optimize a loop a certain way when a break or continue is present. It won't necessarily give a loop that doesn't contain a break as much care in terms of optimization. Also, as Polymorphic said, you have an additional operation that I don't.
master5001 is offline  
Old 12-09-2002, 04:00 AM   #27
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
hmm... i guess i'm better use on my way. everyone will have a lot of way to do the program. as i said, as a good programmer, he /she would follow on the flowchart. goto, continue & break is not recommended to use. but if feel that need to use it, it' okay, but not recommended. yeah, master5001, i got your point. but if you are using break statement, this is not a good sturture in program (for me). but i would another variable the control the loop-statement. anyway, you did find out alot of situation where they did talk about goto, continue & break on the previous threads. but what's the conclusion? they did say that it's bad structure.... bla bla bla. dependent on you which one you prefer.
just one statement, goto, continue & break is not found in flowchart. and if yes, how to flowchart look like? if found out the flowchart look like, this might be unstructure / untidy structure. anyway, dependent on what type want to use ... or you may find out most of the refer book should tell about the comment that i said.

-- beely
beely is offline  
Old 12-09-2002, 04:03 AM   #28
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
Quote:
Originally posted by master5001
Well beely, I tried code that looked something like:

Code:
int i, contains_value = 0;
int error = 0;
size_t size;

for(size = sizeof(array);size-- && !contains_value;)
    if(array[array_size] == target_value)
        contains_value = 1;
And it inlined it (of course to keep things even I inlined mine as well) and the results are 15 secons for your method and 7 for mine. Sorry, but break, continue, and goto have their place in this universe. The bottom line is that your compiler understands that it needs to optimize a loop a certain way when a break or continue is present. It won't necessarily give a loop that doesn't contain a break as much care in terms of optimization. Also, as Polymorphic said, you have an additional operation that I don't.
hmm... may you are right. but i still thinking the break won use it for my whole life (if necessary) on my program
beely is offline  
Old 12-09-2002, 04:13 AM   #29
fou
Guest
 
Posts: n/a
Quote:
Originally posted by beely
Code:
int i, contains_value = 0;

for(i = 0; i < sizeof(array); i++)
    if(array[i] == target_value) {
        contains_value = 1;
        break;
    }
according to your code. it's bad programming althoug it's working. why not try this ??

Code:
int i, contains_value = 0;
int error = 0;

for(i = 0; i < sizeof(array) && error == 0 ; i++)
    if(array[i] == target_value) {
        contains_value = 1;
        error = 1;
    }
--i; // replace back to the value if you wish.
so, decide whether which program is better. for me , i would choose my code. as i said, break OR continue in the for-statement is bad structure. have you seen break or continue in flowchart??? no .. is it??? then why should put for break & continue.
Your code requires an extra compare for each iteration of the loop as well as being less easy to read. If you are reading a couple of million characters (MB) or tens of millions this is a big waste just to avoid a simple break statement. I also use continue as using multiple ifs gets so complicated and so many tabs it's not worth it. Continue is also easier to read.
 
Old 12-09-2002, 04:19 AM   #30
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Hmmm. In my program I added a loop that uses a continue statement and one that uses a goto's. Here are the results:

use condition: 10 seconds
use break: 4 seconds
use continue: 8 seconds
use goto: 0 seconds

It is only fitting that a goto statement would be broken during my test The goto thing would not work properly because I believe the compiler optimized it out.
master5001 is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Help with FAQ JoshG Game Programming 19 10-29-2002 07:31 PM
FAQ Check/Lock RoD A Brief History of Cprogramming.com 2 10-15-2002 11:21 AM


All times are GMT -6. The time now is 03:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22