hhh
hhh

Reputation: 52850

Java: minimum number of operations for conjunctive inequalities?

I try to simplify conditionals in:

for ( int t=0, size=fo.getPrintViewsPerFile().size();
          t<size && t<countPerFile;
          t++)
{
    // ...
}

, more precisely:

t<s && t<c

You need to compare two times, then calc the boolean value from them. Is there any simpler way to do it? If no, how can you prove it? I can simplify it to some extent, proof tree.

[Added]

I tried to solve the problem directly through logic. It would interesting to see the implication in choosing the minima. Link:

http://www.umsu.de/logik/trees/?f=(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts)\lor\neg(Ptc)))

Upvotes: 0

Views: 455

Answers (3)

user207421
user207421

Reputation: 311002

Forget about it. If you're dealing with printing or files this kind of micro-optimizaton would save you practically nothing.

Upvotes: 1

polygenelubricants
polygenelubricants

Reputation: 383816

You can do t < Math.min(s, c), but that wouldn't actually reduce the number of comparison.

I do think that appropriate use of Math.min and Math.max makes for a much more readable code, though. Unfortunately they only have overloads for 2 args (for int, long, float and double arguments). It would've been really nice if they also have 3 args and varargs overloads.

You can always write utilities method for these kinds of things (interval checking is a common idiom (minV <= v) && (v <= maxV) etc), but linguistically, no Java doesn't have any fancy operators that would do these things. They only have basic numerical comparison operators (JLS 15.20.1) and basic boolean operators (JLS 15.22.2, 15.23, 15.24).


Sidenote

Higher-level languages like Icon does allow these kinds of constructs:

Java                      Icon
(x < v1) || (x < v2)      x < (v1 | v2)
(a < b) && (b < c)        a < b < c

Upvotes: 1

interjay
interjay

Reputation: 110158

If size and countPerFile are constant for the duration of the loop, you can precalculate their minimum before the loop, and then the test would be t<minimum.

int size=fo.getPrintViewsPerFile().size();
int minLimit = Math.min(size, countPerFile);
for (int t=0; t<minLimit; t++) {
    ....

Upvotes: 3

Related Questions