how do i simplify this expression?

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,600
4,514
75
Define "simplify". Looks pretty darn simple to me. But what are A, B, C, and D; how are they compared?
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
if ( A==B || A==C || A==D)
...
endif

How much simpler can it get? Are you concerned about performance? Are you worried about evaluating three different huge string variables every time this happens?

You could throw it in a function:

Code:
if CompareVars(a,b,c,d) = 1 then
    ...
end if

function CompareVars(a, b, c, d)
    if a <> b return 0
    if a <> c return 0
    if a <> d return 0
    return 1
end function

Without more details, you can't much simpler than an IF. What are the specific reasons you're trying to optimize such a small bit of code?
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Are you looking for some sort of boolean factoring technique like (A== (B||C||D))? Because no programming language I know works that way (and it really wouldn't make sense to).
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Well, something like this would be slightly faster

if (A == B | A == C | A == D)
do stuff.

It takes advantage of the fact that bitwise operators are faster than logical operators. Since, A==B always equals 1 or 0 this is valid. However, something like if (A | B) could possibly produce some invalid result.
 
Sep 29, 2004
18,656
67
91
Cogman,

What language. I'd think that he java JVM would optimize this at runtime to do what you said.

All you can really do is put the likelihood of failure in order. This is compiler dependent for C/C++ but the Java spec says left to right processing order.

So, for Java, you would put the expression most likely to fail first and least likely last. This way, the case where the expression returns false is calculated in the fastest average time.

This is a micro-optimization though. In the real world, it is not worth the time it takes to think about it.
 

bobross419

Golden Member
Oct 25, 2007
1,981
1
0
Could someone explain the difference between OP and Cogman's code... All I see is that Cogman removed three of the pipes.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Cogman,

What language. I'd think that he java JVM would optimize this at runtime to do what you said.

All you can really do is put the likelihood of failure in order. This is compiler dependent for C/C++ but the Java spec says left to right processing order.

So, for Java, you would put the expression most likely to fail first and least likely last. This way, the case where the expression returns false is calculated in the fastest average time.

This is a micro-optimization though. In the real world, it is not worth the time it takes to think about it.

I was assuming C/C++. It is an interesting setup because most compilers actually won't catch this optimization. I really don't think that the JVM would be any different. They don't catch it because it is actually different. Sort of like using the "restrict" keyword in C++.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Could someone explain the difference between OP and Cogman's code... All I see is that Cogman removed three of the pipes.

Assuming a C/C++ like language, My code uses bitwise ors, the OPs uses logical ors. That means that mine instructs the compiler to just or the results of each equality together while to ops tells the compiler that it must first find the logical equivalent of the results of the equalities before taking the bitwise or. In other words, it has to convert a non 1/0 value into a 1/0 value. (even though the equalities are only capable of returning a 1/0 value).
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,600
4,514
75
Could someone explain the difference between OP and Cogman's code... All I see is that Cogman removed three of the pipes.
Assuming a C/C++ like language, My code uses bitwise ors, the OPs uses logical ors. That means that mine instructs the compiler to just or the results of each equality together while to ops tells the compiler that it must first find the logical equivalent of the results of the equalities before taking the bitwise or. In other words, it has to convert a non 1/0 value into a 1/0 value. (even though the equalities are only capable of returning a 1/0 value).

There's another difference. When testing (x|y|z), all the results have to be computed and or-ed together. When testing (x||y||z), if x is true, y and z don't matter, and so they may not be touched at all. In general with C-like languages:

Code:
if ( A==B || A==C || A==D)
...
endif

is equivalent to

Code:
if( A==B ) goto doit;
if( A==C ) goto doit;
if( A==D ) goto doit;
goto dontdoit;
:doit
...
:dontdoit
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
There's another difference. When testing (x|y|z), all the results have to be computed and or-ed together. When testing (x||y||z), if x is true, y and z don't matter, and so they may not be touched at all. In general with C-like languages:

Code:
if ( A==B || A==C || A==D)
...
endif

is equivalent to

Code:
if( A==B ) goto doit;
if( A==C ) goto doit;
if( A==D ) goto doit;
goto dontdoit;
:doit
...
:dontdoit

In my compilers course we elaborate (a||b) and (a&&b) to (a ? true : b) and (a ? b : false), which I think is somewhat common practice. That representation also performs the short-circuit evaluation.
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
There's another difference. When testing (x|y|z), all the results have to be computed and or-ed together. When testing (x||y||z), if x is true, y and z don't matter, and so they may not be touched at all. In general with C-like languages:

Code:
if ( A==B || A==C || A==D)
...
endif

is equivalent to

Code:
if( A==B ) goto doit;
if( A==C ) goto doit;
if( A==D ) goto doit;
goto dontdoit;
:doit
...
:dontdoit

Yeah, depends on the language. A smart compiler would do it that way, but some interpreted languages will evaluate every expression.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Code:
>>> a=b=c=d=2
>>> a in [b,c,d]
True
>>> a = 1
>>> a in [b,c,d]
False

Python offers the IN operator.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |