Boolean efficiency

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Boolean efficiency

Post by JP »

How can the two following bits of code be done more efficiently:

Code: Select all

if (a == 0 && b == 0) {...}

Code: Select all

if (a != 0 || b != 0) {...}
I was initially quite stumped by it, i didn't realise there were any more efficient ways to do it or that you would ever want more efficient ways. But then i wondered if this is more efficient:

Code: Select all

if (a&b == 0) {...}

Code: Select all

if (a&b != 0) {...}
That halves the number of evaluations of booleans to be done and just adds in a bitwise operator which as far as i know are pretty fast.

I then realised that the & bitwise operator is very similar to + so you coul djust do a+b i guess, which would be quicker? Someone also pointed out that it doesn't work if a == -b.

Any thoughts?
Image Image Image
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Don't design such expressions for "efficiency", design them for readability and correctness.

Even in your most inner loops differences in evaluation costs of these expressions will do *nothing* to your application's performance.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

For this you should have a look for Karnaugh's boolean algebra (it's a standard/must for professional software developers)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Definitely don't write code like that unless it is absolutely necessary. At that point you'd be writing assembly anyways, so readability isn't really an issue.

Travis
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The test on 0 (both == and !=) are usually only one asm statement and (except for the memory loading) also just one cycle. Moreover, both cases can use short circuit evaluation to avoid the second test which might save a complete variable access. Thus, the initial versions can be the fastest. And your compiler will recognize them and optimize if applicable. so stay with it.
Oh, and the bitwise & is wrong: 1&2==0 :!: You would need 1|2
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

:lol: right you are.

This isn't actually for anything in my own programs it's just something that appeared on a test for a company i went to for an interview and i was wondering if anyone knew the answer!

Also i was being very stupid earlier and making things up that weren't true, i think i need to think about this when i have a clear mind and some time to myself :lol:
Image Image Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

LOL, yeah, I think this was ment to test for Karnaugh-Veitch knowledge !!! :lol:
As I mentioned it's developers standard knowledge !!! :wink:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
CodeDog
Posts: 106
Joined: Sat Oct 07, 2006 8:00 pm
Location: CA. USA
Contact:

Post by CodeDog »

Acki wrote:LOL, yeah, I think this was ment to test for Karnaugh-Veitch knowledge !!! :lol:
As I mentioned it's developers standard knowledge !!! :wink:
More accurately is that it is taught as part of a standard computer science degree program but is never used in the real world outside of academia (unless you are making compiler software).
As mentioned readability and correctness are vastly more important.
Never solve a performance problem until you actually have a performance problem.
If you really need to increase an applications performance you would do better minimizing hard disk/Network/Database reads and writes.

When I look to hire a programmer I have a list of questions I use to see if they are "exaggerating" their number of years of experience. They are not the perfect set of questions but I have found that they quickly help me identify people that "claim" to be an experienced programmer but actually have "NO" real world experience.

Questions
These are concept questions, exact syntax is not required.

1.
float fNum = 1.79;
int iNum = 0;
iNum = (int) fNum;

What is the value of iNum?

2. Make a GUI Window on the screen with C++ in any manner you want. (Not a console app)

3. How would you iterate through a collection in C++?

4. How would you iterate through a collection in C#?

5. Name a control that you could use to display data on an ASP.NET 2.0 web application page.

6. Write an SQL statement that would list all the records from a table named TableA.

7. In C++ from inside a function how would you change the Title of a window?

8. There are 2 tables, TableA and TableB with the following structure:
TableA
TableAID |autonumber|
Field1 |…|
Field2 |…|
… |…|

TableB
TableBID |autonumber|
TableAID |long|
Write an SQL statement that would list all the records in TableA that don’t have a match in TableB.

9. In a VB6 or VBA function how would you trap an error?

10. In C++ how would you trap an error?

11. In C# how would you trap an error?

12. In a multithread GUI application written in MFC how do you send a message from the worker thread to the main process thread?

13. When using the ADO com control that comes with Windows XP what is the data type returned from a fields get item method call?

14. In a C# .NET Web Application how would you make some type of variable accessible to multiple pages (For example: username and password).

15. You have a Default.aspx that prompts for a login and after a successful login it takes the user to main.aspx which displays data. Describe how you would prevent a user from bypassing Default.aspx by manually typing main.aspx into the address bar.

16. On a web form you have a textbox where the user can type the first few letters of a person’s last name and then the GridView will display all the people whose last name begin with the search string.

a. How would you make sure the user types at least 3 letters?
b. What do you add to the string the user typed before submitting it as part of an SQL query?
c. How would you prevent a user from harvesting the entire database with injection?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I think you are working with Windows far too much. C++ does not provide a GUI framework :!: So concepts are not possible at all. And SQL is not a programming language... Nor is VBA :wink:
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Well, if he wants windows developers, he better asks windows questions. And eventhough SQL is no programming language, I guess for working at a database project it might help to know a bit. ;)

But CodeDog, you'd sieve me out anyway, because I don't know VBA/ASP/ADO. :) My questions would look different probably, though I've never been on this side of an interview. Too little about concepts and most of the part too easy.
CodeDog
Posts: 106
Joined: Sat Oct 07, 2006 8:00 pm
Location: CA. USA
Contact:

Post by CodeDog »

Your right, I only hire windows developers. They don't have to answer all the questions, just the ones that relate to the areas they claim to have experience in. You see, I am not testing them for breath of knowledge, but for lying. Kids fresh out of school fall for it every time. An experienced programmer will find the questions in his area trivially easy and will just but NA or something similar on questions outside his area, a faker will try and guess in order to hide lack of knowledge.
You would be surprised at the number of people I see that claim to be experienced windows developers that are trying to fake their way into their first programming job.

If you went to someone that claimed to be a car mechanic with an alternator in your hand and he didn't know what it was, would you want him working on your car?

In other words, if someone is applying for a windows developer position and he has no idea how to create a window on the screen then I don't need to waste time interviewing him.
Post Reply