Suppose I have a bunch of integers (10~20) and need to check if any of them equals 0. What's the most efficient way to do it? I don't want to evaluate a giant if(a=0 || b=0 || c=0 ||...) statement. I thought of if(abc... = 0) but if I remember correctly multiplication isn't a very quick process. Are there any other tricks, such as bit wise operations that would work? I'm trying to think as low level as possible to make this super efficient.
I'm pretty sure the fastest and clearest way to do this is with an explicit test:
int has_zero = !a || !b || !c || !d || !e ...;
Because the || and && are short-circuiting operators in C, evaluation stops as soon as the final result is known, so if (for instance) the b variable is zero, that satisfies the expression as true and stops evaluating the rest.
#AbhayAravinda suggested that !(a && b && c && d ...) might be more efficient, but I don't think so; because this is not so much doing an explicit not operation, but a low-level test-against-zero, this is a really easy test for pretty much any architecture to do reliably. I did a quick look at optimized assembler for both versions and there was no clear winner for performance, but I think the first version is clearer.
If every single cycle matters, then check both versions on your platform, but on my 64-bit Intel system, both gcc and clang do in fact generate the same assembly for both versions (with optimizations turned on).
Simple test code:
int a, b, c, d, e, f;
int test_or()
{
return !a || !b || !c || !d || !e || !f;
}
int test_and()
{
return ! (a && b && c && d && e && f);
}
int main()
{
return test_or() | test_and();
}
Compile this with gcc -S -O testfile.c and look at the resulting .s file.
Test each one in turn. Exploit the short-circuiting property of ||; place the variables in descending order of the probability of each being zero:
if (!a/*most likely to be zero*/ || !b || ...){
// one of them is zero
}
Most people give an answer like:
!a || !b || ...
(where a is the most probable one of being zero)
The idea is that, in case a is zero, then the rest of the sequence is not evaluated (because of not being necessary), which is a kind of optimisation, performed by the compiler.
This turns the question into: does your compiler perform this optimisation or not (and in case of "possibly yes", what are the parameters in order to enforce this)?
Can you tell us which compiler (version) you're working with? This might enable us verifying this.
You may look at the assembler output.
The !a || !b || !c || !d || !e || !f will give you a bunch of cmp and je statements. One pair for each variable. Because of boolean short cut evaluation, it may run very fast. Or not.
The maybe better and deterministic solution is using the bitwise AND operator. If one operand is 0, then the result will be 0. So someting like:
if (a & b & c & d & e & f & g & h & i & j & k)
will result in one mov and then and statements for each variable.
So, if the variable that is 0 is in the 2nd half of the if statement, then the bitweise AND will be faster.
Related
I had the task of finding a logical expression that would result in 1 if and only if a given number n is a multiple of 2019 and is NOT from the interval (a, b).
The textbook gave the following answer and I don't really understand it:
a>=n || b<=n && (n%3==0 && n%673==0)
The thing between those parantheses I understand to be equivalent to n%2019==0, so that's alright. But I don't understand why this works, I mean the && operator has higher priority that the || operator, so wouldn't we evaluate
b<=n && (n%3==0 && n%673==0)
first and only at the end if n<=a? I thought that if I were to do it, I would do it like this:
(a>=n || b<=n) && (n%3==0 && n%673==0)
So I just added that extra set of parantheses. Now we would check if the number is not in the interval (a, b), then we would check if it is a multiple of 2019 and then we would 'and' those to answers to get the final answer. This makes sense to me. But I don't understand why they omitted that set of parantheses, why would that still work? Shouldn't we consider that && has higher priority than ||, so we add an extra set of parantheses? Would it still work? Or is it me that is wrong?
Trying it out shows that the expression as written without the extra parentheses doesn't work:
bool expr(int n, int a, int b)
{
return a>=n || b<=n && (n%3==0 && n%673==0);
}
expr(1000, 2000, 2018) for example evaluates to true, even though it is not a multiple of 2019.
As you pointed out, the logical AND operator && has higher precedence than the logical OR operator || (reference), so the expression is equivalent to:
a>=n || (b<=n && (n%3==0 && n%673==0))
which is always true when n <= a, even if it's not a multiple of 2019.
A clearer expression would be:
(n % 2019 == 0) && (n <= a || n >= b)
New here. I was just wondering if it's possible to make this if statement shorter and less redundant.
if (!a && b)
{
if (c == d && e > 0)
{
return;
}
}
else if (a && !b)
{
if (c != d)
{
return;
}
}
else if (!a && !b)
{
return;
}
Here's what I've ended up with
if ((!a && b && c == d && e > 0) || (a && !b && c != d) || (!a && !b))
{
return;
}
All I did was join nested if statements with an && operator, and if-else if statements with || operator. Now I'm stuck, is it possible to make this even shorter? If you could share some tips or your way of approaching this kind of scenario, I'd be glad to hear it out.
Neither of the approaches is readable. It will be better to create a function that indicates the kinds of checks you are performing.
if ( !my_very_sensible_special_conditions_are_met(a, b, c, d, e) )
{
return;
}
After that, whether you use the first approach or the second approach in the implementation of the function is less of an issue.
condition ladder(if,else if) shortening
As you mentioned, in all blocks(if, else if) if you are just having the same set of statements then you can put all the conditions with a single if statement with proper conditions with appropriate brackets.By doing this lines of code will get reduced But you will have some disadvantages as per my point of view
Readability will get reduced
In future, if you want to change your code for a particular condition, it will take some time to change.it is not that much easy as you do in condition ladder
if you are not having any problem with the above two points, you can go with your approach itself.But code your conditions as atomic as possible.it will help you out in a long run.
Given the number and complexity of conditions, I'd at least consider making it table driven. For the moment, I've assumed that a and b are actually ints containing either a 0 or a 1.
int index = a | (b<<1) | (int(c==d)<<2) | (int(e>0) << 3);
static const bool should_return[] = {
1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
};
if (should_return[index])
return;
The table is still basically incomprehensible, but at least it encodes that incomprehensibility fairly compactly, and makes it easy for the code to use it.
Since you commented that you are always returning, and since your code does not specify if you do anything else in these if - else cases why not do just
if (! (a && b) )
{
return;
}
You can shorten it a tiny bit further:
if (!a && !b || !b && c != d || !a && c==d && e> 0) {
return;
}
But that doesn't increase readability and most likely also not performance. What I would focus on is, what reads most natural with the real variable names.
As a general advice, I'd start with checking the simpler / shorter conditions first. That makes it often more easier to see if you make redundant checks in the following ones.
In any case, as #R.Sahu suggested, a complex condition like that belongs into a separate, named and commented function. If you don't find a good name for it use something generic like e.g. check_<main_function>_preconditions.
EDIT:
Sorry, I can't resist: If you wan to go overboard, you can make it pretty compact by throwing in a trinary statement:
if (!a && !b || c != d ? !b : !a && e> 0) {
return;
}
DON'T do that in production code!
I have some trouble understanding Bitwise-And and Unary Complement when both are used in this code snippet
if((oldByte==m_DLE) & (newByte==m_STX)) {
int data_index=0;
//This below line --- does it returns true if both the oldByte and newByte are not true
//and within timeout
while((timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))) {
if(Serial.available()>0) {
oldByte=newByte;
newByte=Serial.read();
if(newByte==m_DLE) {
.
.
.
are the both operators & ~are performing a logical not operation like checking until if both oldByte and newByte are false
The above code is from the link --> line 227 of the code
I am trying to use the implement the code for my application in C but without the timing functions
if((oldByte==DLE) && (newByte== STX)) {
data_index = 0;
// is this the correct implematation for above C++ code to C
while(! ((oldByte== DLE) && (newByte== ETX))){
oldByte = newByte;
Is this method correct for implementing in C
(timeout.read_s()<m_timeout) & ~((oldByte==m_DLE) & (newByte==m_ETX))
is equivalent to (but probably less readable than)
(timeout.read_s()<m_timeout) && !(oldByte==m_DLE && newByte==m_ETX)
which is equivalent to (and IMO less readable than)
(timeout.read_s()<m_timeout) && (oldByte!=m_DLE || newByte!=m_ETX)
Edit: should add a caveat about short-circuiting. Although the particular example statements will all return the same value, using && or || will skip evaluating pieces that can't impact the result. This isn't important in your specific example, but could be very important in an example like this:
(oldByte!=nullptr & *oldByte == m_ETX) // will crash when oldByte=nullptr.
(oldByte!=nullptr && *oldByte == m_ETX) // will evaluate to false when oldByte=nullptr.
Since the equality-operator (==) yields 0 or 1 as a result, you can use bitwise and, too. (foo==1) & ~(bar==1) works too, since the AND with (foo==1), which always results in 1 and 0, masks all other bits in ~(bar==1). However, it is strongly recommended to use the logical counterparts &&, || and !.
The following would not work as expected:
if (~(bar == 1) & ~(foo == 1))
e.g. if foo = bar = 1, then it would evaluate to 0xfffffffe on ia32, which is different from 0 and therefore "TRUE"
I didn't think these if's would compile but they do:
if (a>>b&&c&&d)
if (month==1,2,3,5,7,9,10)
The first I'm clueless about. In the second statement is the comma supposed to be an (||) or operator ?
Syntax wise was it always this way or was it introduced some time ago ?
I'm using Visual Studio 2010.
if (a>>b && c && d)
it is equal to
if ((a>>b) && c && d)
if the result of a shifted right b times evaluates to a bool, c and d also evaluates to bool respectively, then all these booleans will be AND-ed to each other.
In your context, the all expressions within commas will be evaluated and then the last expression will be passed to if expression:
if (month==1,2,3,5,7,9,10) -> is equal to
if (2,3,5,7,9,10) -> is equal to
if (3,5,7,9,10) -> is equal to
if (5,7,9,10) -> is equal to
if (7,9,10) -> is equal to
if (9,10) -> is equal to
if (10)
which is always true.
It's not suppose to be || or &&. If you want OR or AND write it like below:
if (month==1 || month==2 || month==3 || ....)
or
if (month==1 && month==2 && month==3 && ....)
// Also month can not simultaneously be equal to more than one value!
// then, it's equal to
if (false)
The first if statement would be evaluated like:
if(((a >> b) && c) && d)
Essentially bitshift a by b bits and then logical and with c and then with d
The second is the comma operator which will evaluate the first term and throw it away, then the second, and so on and return the result of the final term. So in our case the statement is equivalent to:
if(10)
which is always true.
bool OrderUtils::shouldCptyAutoexecute(int Id)
{
bool res =
dummyCache::instance().getVal(Id).getWhitelabelType() == "ABCD";
if (!res)
res |= dummyCache::instance().getVal(Id).getIsPlc() == 1;
return res;
}
The above code checks for 2 Id's and returns true to res if any of the id is present in the database.
Can you suggest a way in which I can compare one more value from the databse table and return true to the value res..Also can you explain what does the second if statement do and the bitwise OR operator?
Sir, just let the short-circuit eval do this for you:
return dummyCache::instance().getVal(Id).getWhitelabelType() == "ABCD" ||
dummyCache::instance().getVal(Id).getIsPlc() == 1;
If the first is true, the second will not fire. Moreover, I assure you a remotely-reasonable optimizing compiler will not re-fire instance().getVal(id) if the returned object has not changed between the getWhitelabelType() and getisPlc() calls. In fact, i would all-but-guarantee it if getWhiteLabelType() is const. (and it certainly looks like it should be).
Regarding the bit work. The expression was pretty-much whacked. though it will work. Unless I read it wrong (and trust me, the list of people that will tell me I am will let me know quickly) it is performing a boolean eval, promoting the resulting true/false bool to an int, promoting the current value of res from bool to int (which is zero, so nothing special there), bitwise-OR-ing that with the expression int, then demoting the final int back to a bool to store in res . I'm surprised this doesn't flag at least a warning on the compiler.
It likely should have been if (!res) res ||= expr, and even then, it is pointless, as you can just use short circuit eval as in the top of this answer to cut out the local res entirely. Consider if res were false. Then the equivalent expression would be res = false || expr. But thats just res = expr. In the !res state it executes in, you may as well just use an assignment.
Finally, regarding adding a third field to your eval, it depends entirely on how you want it added. for an additional logical OR it is pretty simple.
For an expression like (A || B || C) you can just
return dummyCache::instance().getVal(Id).AField() == ATestValue ||
dummyCache::instance().getVal(Id).BField() == BTestValue ||
dummyCache::instance().getVal(Id).CField() == CTestValue;
For more complex operations, some judicious use of parenthesis will go a long way. For example, to return (A || B) && C:
return (dummyCache::instance().getVal(Id).AField() == ATestValue ||
dummyCache::instance().getVal(Id).BField() == BTestValue) &&
dummyCache::instance().getVal(Id).CField() == CTestValue;
Or perhaps (A && C) || (B && !C) (ok this is getting a little overboard...)
return (dummyCache::instance().getVal(Id).CField() == CTestValue)
? (dummyCache::instance().getVal(Id).AField() == ATestValue)
: (dummyCache::instance().getVal(Id).BField() == BTestValue);