The performance of a solution with 'if' should be similar to the native function performance.If it were a native function, it would be faster than using If Statements.
The performance of a solution with 'if' should be similar to the native function performance.
The native function does exist, it's:
Signum(d)
in Android.
Best regards.
i tried it but it doesnt recognize the function "Signum()" . do i need to set a reference to an math-library?
r.Target=r.CreateObject("java.lang.Math")
r.Target=r.RunMethod2("signum", number, "java.lang.double")
Dim num As Float
num = -50.0
Dim r As Reflector
result=r.RunStaticMethod("java.lang.Math","signum",Array As Object(num),Array As String("java.lang.float"))
Log(Result)
Dim timestart, signtime, rsigntime As Long
Dim n As Int
' Test Sign function using Reflection
timestart = DateTime.Now
For n = 0 To 100000
RSign(-20)
Next
rsigntime = DateTime.Now - timestart
' Test Sign function using Sub
timestart = DateTime.Now
For n = 0 To 100000
Sign(-20)
Next
signtime = DateTime.Now - timestart
' Log the result
Log("Sign: " & signtime & " RSign:" & rsigntime & " rel:" & Round(rsigntime/signtime))
Sub RSign(num As Float) As Int
Return r.RunStaticMethod("java.lang.Math", "signum", _
Array As Object(num), Array As String("java.lang.float"))
End Sub
Sub Sign(d As Double) As Int
If d = 0 Then
Return 0
Else
Return(d / Abs(d))
End If
End Sub
Sign:27 RSign:4790 rel:177
Sign:27 RSign:5028 rel:186
Sign:22 RSign:2950 rel:134
Sign:19 RSign:2980 rel:157
Sign:33 RSign:3701 rel:112
Sign:21 RSign:2899 rel:138
Taking out divide, it returns results 1.44 times faster.
Sub sign(d As Double) As Int
If d = 0 Then Return 0
If d > 0 Then Return 1
Return -1
End Sub
R:19186 signND:100 signND:52 signNDNE:54
R/sign:192 R/signND:369 R/signNDNE:355
---
R:13462 signND:100 signND:48 signNDNE:49
R/sign:135 R/signND:280 R/signNDNE:275
---
R:13402 signND:97 signND:48 signNDNE:48
R/sign:138 R/signND:279 R/signNDNE:279
---
R:14145 signND:97 signND:48 signNDNE:49
R/sign:146 R/signND:295 R/signNDNE:289
---
R:13666 signND:99 signND:47 signNDNE:48
R/sign:138 R/signND:291 R/signNDNE:285
---
R:13576 signND:97 signND:49 signNDNE:48
R/sign:140 R/signND:277 R/signNDNE:283
---
R:13561 signND:98 signND:49 signNDNE:48
R/sign:138 R/signND:277 R/signNDNE:283
---
R:13880 signND:119 signND:50 signNDNE:50
R/sign:117 R/signND:278 R/signNDNE:278
---
R:13914 signND:97 signND:48 signNDNE:48
R/sign:143 R/signND:290 R/signNDNE:290
---
R:13584 signND:100 signND:50 signNDNE:49
R/sign:136 R/signND:272 R/signNDNE:277
--------------
R:14237 sign:100 signND:48 signNDNE:49
R/sign:142 R/signND:297 R/signNDNE:291
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?