B4J Question How to do division of two Ints as integer division instead of floating point division?

emexes

Expert
Licensed User
B4X:
Dim Temp As Int = NewX * 2 + 1
Dim Temp1 As Int = Temp * SrcWidth
Dim Temp2 As Int = (NewWidth * 2)
Temp = Temp1 / Temp2

decompiles to:

Java:
int var23 = var21 * 2 + 1;
int var24 = var23 * var17;
int var25 = var1 * 2;
int var10000 = (int)((double)var24 / (double)var25);

Is there any way to get the castings out of the division line, without using inline Java?
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
@William Lancee : I only see about a 2:1 difference (Java almost twice as fast as B4X), but I also changed the code a bit, since B4X has no ++ operator:

B4X:
public Sub b4xIntDiv(a As Int, b As Int, loopCount As Long) As Int
    Dim result As Int
    Dim counter As Long
    Do While counter < loopCount
        a = a + 1
        b = b + 1
        result = a / b
        counter = counter + 1
    Loop
    Return result
End Sub


#IF JAVA
    public static int JavaIntDiv (int a, int b, long loopCount){
    int result = 0;
    for (long counter=0; counter < loopCount; counter++) {
        a = a + 1;
        b = b + 1;
        result = a / b; // result will be 7 / 3 = 2 and 4 / 3 = 1

       
    }
    return result;
    }

public static int JavaIntDiv2 (int a, int b, long loopCount){
    int result = 0;
    long lc = 0;
    while (lc++ < loopCount){
        a = a + 1;
        b = b + 1;
        result = a / b; // result will be 7 / 3 = 2 and 4 / 3 = 1

        }
    return result;
    }

#End If

One of the results:

Executing B4X's "integer" division 100000000 times
b4xIntDiv took 595 ticks to complete 100000000 operations
Executing Java's integer division 100000000 times
Java took 356 ticks to complete 100000000 operations
Executing Java's integer division (while loop) 100000000 times
Java (while loop) took 304 ticks to complete 100000000 operations

The generated Java code for the B4X routine is as follows:
Java:
public static int  _b4xintdiv(int _a,int _b,long _loopcount) throws Exception{
int _result = 0;
long _counter = 0L;
 //BA.debugLineNum = 66;BA.debugLine="public Sub b4xIntDiv(a As Int, b As Int, loopCount";
 //BA.debugLineNum = 67;BA.debugLine="Dim result As Int";
_result = 0;
 //BA.debugLineNum = 68;BA.debugLine="Dim counter As Long";
_counter = 0L;
 //BA.debugLineNum = 69;BA.debugLine="Do While counter < loopCount";
while (_counter<_loopcount) {
 //BA.debugLineNum = 70;BA.debugLine="a = a + 1";
_a = (int) (_a+1);
 //BA.debugLineNum = 71;BA.debugLine="b = b + 1";
_b = (int) (_b+1);
 //BA.debugLineNum = 72;BA.debugLine="result = a / b";
_result = (int) (_a/(double)_b);
 //BA.debugLineNum = 73;BA.debugLine="counter = counter + 1";
_counter = (long) (_counter+1);
 }
;
 //BA.debugLineNum = 75;BA.debugLine="Return result";
if (true) return _result;
 //BA.debugLineNum = 76;BA.debugLine="End Sub";
return 0;
}

Notice how everything seems to be casted to its resulting value.

So, with the speed difference over a large set of calculations (really large), would B4X benefit from a dedicated integer division operator? Would it be worth the additional language change?
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
So, with the speed difference over a large set of calculations (really large), would B4X benefit from a dedicated integer division operator? Would it be worth the additional language change?

Yes, especially if a Java function call has to be made each time a calculation is to be done.
I tested making individual calls and it increased the total time from 116 ticks to 3564 ticks on my benchmark of 100000000 iterations.

Note: differences between our testing is probably due to differences in our computer systems.
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
The B4X way just has too many casting. In my knowledge casting is slow. For normal app it is trival but if computation is very heavy I may use inline java or call C/C++ dll/so.
 
Upvote 0
Top