"try" or not to "try"?

pliroforikos

Active Member
Licensed User
I am talking about command try - catch

Which code is faster? For example:
B4X:
try
    val = mymap.get("Not_existing_Value")
catch
    log(LastException)
end try

' Or
if mymap.ContainsKey("Value") then val = mymap.get(value)

Imagine i have to check 1000 keys for existence.
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
The try catch in your example won't raise an error. Reading a non existant key simply returns null.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Adding the needed code for the left side of the assignment:
The .GetDefault is 20 times faster then the Try ... End Try (tested in Release)

B4X:
    Dim mymap As Map
    mymap.initialize
    Dim markTime As Long = DateTime.Now
    For i = 1 To 10000
        Try
            Dim test As Int = mymap.get("Not_existing_Value")
        Catch
            'Log(LastException)
        End Try
    Next
    Log(DateTime.Now - markTime)        '20 milliseconds for 10,000 iterations
    markTime = DateTime.Now
    For i = 1 To 10000
        Dim test As Int
        If mymap.ContainsKey("Value") Then test  = mymap.getDefault("Not_existing_Value", 0)
    Next
    Log(DateTime.Now - markTime)        '1 milliseconds for 10,000 iterations
 

William Lancee

Well-Known Member
Licensed User
Longtime User
And after removing the redundant if statement, it is 75 times faster.
I had to increase # of iterations to 100000. Map lookups are superfast.

B4X:
    Dim mymap As Map
    mymap.initialize
    Dim markTime As Long = DateTime.Now
    For i = 1 To 100000
        Try
            Dim test As Int = mymap.get("Not_existing_Value")
        Catch
            'Log(LastException)
        End Try
    Next
    Log(DateTime.Now - markTime)        '150 milliseconds for 100,000 iterations
    markTime = DateTime.Now
    For i = 1 To 100000
        Dim test As Int = mymap.getDefault("Not_existing_Value", 0)
    Next
    Log(DateTime.Now - markTime)        '2 milliseconds for 100,000 iterations
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Both of the following affect the speed:

1. The map in the code above is empty
2. The contents of the loop doesn't change

When I preload the map with random numbers (0 - 1000000), and vary the none-existing value to random numbers...
I get this, still quite a difference, but the try loop was not affected - implying optimization for static loops.

In the real scheme of things 11msec for 100,000 lookups is not shabby.

B4X:
    Dim mymap As Map
    mymap.initialize
    For i = 1 To 100000
        mymap.Put(Rnd(0, 1000000), True)
    Next
    
    Dim markTime As Long = DateTime.Now
    For i = 1 To 100000
        Try
            Dim test As Int = mymap.get(1000000 + Rnd(0, 1000000))
        Catch
            'Log(LastException)
        End Try
    Next
    Log(DateTime.Now - markTime)        '152 milliseconds for 100,000 iterations
    markTime = DateTime.Now
    For i = 1 To 100000
        Dim test As Int = mymap.getDefault(1000000 + Rnd(0, 1000000), 0)
    Next
    Log(DateTime.Now - markTime)        '11 milliseconds for 100,000 iterations
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…