Sep 1, 2017 Replies: 7 To obtain this behavior in B4X, you can do the following: 1. Declare a global Map Code: Copy Sub Process_Globals ... Dim TF as Map ... End Sub 2. Initialize the Map Code: Copy Sub Activity_Create(...) ... TF.Initialize TF.Put(False, 0) TF.Put(True , 1) ... End Sub 3.Enjoy Code: Copy Sub isOver9000(powerLevel As Int) As Boolean Return powerLevel > 9000 End Sub Log(TF.Get(isOver9000(23847)) '1 Log(TF.Get(isOver9000( 589)) '0 Click to expand...
To obtain this behavior in B4X, you can do the following: 1. Declare a global Map Code: Copy Sub Process_Globals ... Dim TF as Map ... End Sub 2. Initialize the Map Code: Copy Sub Activity_Create(...) ... TF.Initialize TF.Put(False, 0) TF.Put(True , 1) ... End Sub 3.Enjoy Code: Copy Sub isOver9000(powerLevel As Int) As Boolean Return powerLevel > 9000 End Sub Log(TF.Get(isOver9000(23847)) '1 Log(TF.Get(isOver9000( 589)) '0
stevel05 Expert Licensed User Longtime User Sep 1, 2017 #2 Or as a sub: Code: Copy Public Sub TF(B as Boolean) as Int If B Then Return 1 Return 0 End Sub
wonder Expert Licensed User Longtime User Sep 1, 2017 #3 stevel05 said: Or as a sub Click to expand... 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 said: Or as a sub Click to expand... 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 Sep 1, 2017 #4 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 CallSubSpeedTest.zip 6.8 KB · Views: 478
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.
stevel05 Expert Licensed User Longtime User Sep 1, 2017 #5 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 CallSubSpeedTest.zip 6.8 KB · Views: 458
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
wonder Expert Licensed User Longtime User Sep 1, 2017 #6 So the function call seems to be way faster in Release Mode...
stevel05 Expert Licensed User Longtime User Sep 1, 2017 #7 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: Sep 1, 2017
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.
OliverA Expert Licensed User Longtime User Sep 2, 2017 #8 wonder said: So the function call seems to be way faster in Release Mode... Click to expand... 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.
wonder said: So the function call seems to be way faster in Release Mode... Click to expand... 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.