Android Code Snippet Boolean as Int (as in C/C++/Python)

To obtain this behavior in B4X, you can do the following:
1. Declare a global Map
B4X:
Sub Process_Globals
    ...
    Dim TF as Map
    ...
End Sub
2. Initialize the Map
B4X:
Sub Activity_Create(...)
    ...
    TF.Initialize
    TF.Put(False, 0)
    TF.Put(True , 1)
...
End Sub

3.Enjoy
B4X:
Sub isOver9000(powerLevel As Int) As Boolean
    Return powerLevel > 9000
End Sub

Log(TF.Get(isOver9000(23847)) '1
Log(TF.Get(isOver9000(  589)) '0
 

wonder

Expert
Licensed User
Longtime User
Or as a sub
Yes indeed, but using a Map should cause less overhead than a function call...
...well, at least in theory, no one really knows how Java works.
 

stevel05

Expert
Licensed User
Longtime User
Yes, just tested it. On B4a with my Nexus 7 it appears that accessing a map is twice as fast. On B4j, it's the other way round.
 

Attachments

  • CallSubSpeedTest.zip
    6.8 KB · Views: 301

stevel05

Expert
Licensed User
Longtime User
A bit more testing. Removed the log statements and increased iterations.

On B4a in Debug mode Avg is roughly: Map: 8ms FunctionCall: 42ms
On B4a in Release mode Avg is roughly: Map: 10ms FunctionCall: 4ms

On B4j in Debug mode Avg is roughly: Map: 7ms FunctionCall: 8ms
On B4j in Release mode Avg is roughly: Map: 7ms FunctionCall: 1ms
 

Attachments

  • CallSubSpeedTest.zip
    6.8 KB · Views: 299

stevel05

Expert
Licensed User
Longtime User
It looks that way, I didn't really do it too scientifically and only ran the tests 7 or 8 times. But the results appear to be similar each time.
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
So the function call seems to be way faster in Release Mode...
I'm going to add this to my list of unwritten rules (since I've had some issues with this too):

Rule: Do not run benchmark tests in Debug Mode, unless you intend to benchmark Debug Mode.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…