| Author |
Booleans in conditional statements.
|
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
Hi all. Not sure if this is the right place... but anyway. I was just about to write (in a document) that a Boolean in a conditional expression would improve performance over the use of an expression. For example: Then I was going to ask if it really is more efficent to use a boolean, but I think in posing my question I have answered it for myself. Here goes: All conditional expressions should evaluate to a boolean value. In using an expression to control the conditional statement above, the expression must first be evaulated to a TRUE or FALSE boolean, and then the conditianal statement (if) most be evaluated. So by using a boolean rather then an expression in the conditional statement, I am "skipping" to evaluating the conditional statement, rather then the expression and the statement. Thus it is more efficnet to use a boolean where possible. I hope? Thanks Gavin
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Gavin Tranter: So by using a boolean rather then an expression in the conditional statement, I am "skipping" to evaluating the conditional statement, rather then the expression and the statement. Thus it is more efficnet to use a boolean where possible. I hope?
But you would still have to set your boolean variable somewhere and this would be done by executing a statement. You do realise that in your first example, x will always be false, so it could be optimised even more to just In your second example you have the choice of either doing it as you have done or you could evaluate the statement first and then test the result However, I think this would only make a difference in performance if you are testing the result several billion times in your program. Or have I completely misunderstood your question ? If so, can you post a clearer example.
|
Joanne
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
Hi Joanne, No I dont think you have miss understood my question. The values for both the boolean and the char would be comming for a DB, so the cost of each would be roughtly equal. It could be that we have several million rows in that DB table. I am trying to argue that using a char (Y and N) as a boolean value in a DB is "bad" for a number of reasons, one of them being the evaluation outlined above. Sorry about the confussing code example, it was there just to show the two types of conditional statement I was talking about, I was confusing myself so i figured an example might clear things up for everybody, esp me. A clearer example might be: Gavin [ March 05, 2007: Message edited by: Gavin Tranter ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
So your question reduces to "does evaluating an expression like "x == 'N'" take zero time?" and the answer is, of course, no. Evaluating an expression takes a finite amount of time. But as Joanne says, it's an infinitesimal amount of time, so little time that you should not worry about unless it's inside the tightest of loops, to be executed billions of times, where every instruction counts. In the typical case, as for most code, the most important consideration is clarity. And that said, if a database supports a boolean type, and you're storing a Boolean value, then use the boolean type rather than a char column containing 'Y' or 'N' not because it's faster, but because it's clear, unambiguous, and requires no documentation of a convention.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
What I was asking was, is if(boolean) "quicker" then if(someValue=='Y'), to which i decided it was, by dent of if(boolean) dosnt have to evaulate someValue=='Y' to boolean before evaluating the statement. I think, if I understood Joanne's andswer and your answer, yes it is quicker, buit only by the the smallest of fractions. I was just checking that my "logic" was correct. Using the DB native boolean being more readable, both in the DB and in code, is one of my arguments also It turns out that Oracle dsont have a native boolean type. Which is a bit annoying. As I personal thinking using a byte or char for a boolean has data quality issues.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
|
I think Joanne's other point was that the boolean espression has to be evaluated somwhere unless its a constant literal. So you are not really saving anything. That is whether the boolean value is evaluated in the conditional or elswhere, if it is not a constant then you are just moving the evaluation and not really saving any cycles.
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
|
Yes, I understood that, I was assuming (a dangrous past time I know) that as value would be retrieved from the DB, that I could treat them as cosntant expressions so to speak.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1170
|
|
Originally posted by Gavin Tranter: What I was asking was, is if(boolean) "quicker" then if(someValue=='Y'), to which i decided it was, by dent of if(boolean) dosnt have to evaulate someValue=='Y' to boolean before evaluating the statement...
Yes. Yes your logic is correct. This may eventually lead you to pulling such a comparison outside of a loop so its not performed unnecessarily every time. Under certain circumstances the runtime compiler may do this for you.
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
Originally posted by Joanne Neal: In your second example you have the choice of either doing it as you have done or you could evaluate the statement first and then test the result However, I think this would only make a difference in performance if you are testing the result several billion times in your program.
I think this part of Joanne's post answers my post very well. However I think my original post/question was not very clear in the first place, sorry.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Garrett Rowe: I think Joanne's other point was that the boolean espression has to be evaluated somwhere unless its a constant literal.
That was the point I was making. However, as it turns out, the evaluation is not being done elsewhere. The OP has the choice of fetching a boolean or a char from a database. If he fetches a char then he has to do a comparison to produce a boolean. If he fetches a boolean, no comparison is needed. Having said all that, I think he will probably find that that the difference between using a boolean and doing a compare is infinitesimally small when combined with an operation to fetch data from a database.
|
 |
 |
|
|
subject: Booleans in conditional statements.
|
|
|