I think 1/4 of the time is taken by thinBasic just for interpreting and calling O2 function in the loop:
For i=1 To 10000
For j=2 To 50
x=Zeta(j)
Next
Next
Below code is 100% O2 code and it takes about 600 ms instead of 800 ms
I moved constant declarations outside zeta function (they are evaluated just once during O2 just in time compilation.
Created a test lRun function to see how much time was taken by thinBasic for interpretation in the main loop.
Uses "Console" , "oxygen"
Dim i , j, t As Long
Dim scr As String
Dim As Long pZeta
Dim As Long pRun
Dim As Long pFinish
Dim x, y As Double
'---O2 code
scr = "
Dim As Double c(7)
c(1) = 8.3333333333333330e-02 'B(2)/2!
c(2) = -1.3888888888888890e-03 'B(4)/4!
c(3) = 3.3068783068783070e-05 'B(6)/6!
c(4) = -8.2671957671957670e-07 'B(8)/8!
c(5) = 2.0876756987868100e-08 'B(10)/10!
c(6) = -5.2841901386874930e-10 'B(12)/12!
c(7) = 1.3382536530684680e-11 'B(14)/14!
function zeta(byval n as double) as double link #pZeta
'Riemann zeta function evaluated using the Euler-Maclaurin summation formula.
'first we sum the first ten terms of the Zeta series,
'then use the Euler-Maclaurin summation formula.
Dim As Integer i, k, k1, nc
Dim As Double s, dx, n1
nc=10
s=0
For i=1 To nc
s=s+1/i^n
Next
'f(k)=(k+10)^-n
s=s+nc^(1-n)/(n-1) 'int(f(k)dk,k=0..infinity)=10^(1-n)/(n-1)
dx=-n*nc^(-n-1) 'first derivative of f(k)
s=s-.5*nc^(-n) 'f(0)*B(1), first term of the Euler-Maclaurin summation formula.
n1=nc^(-2)
k=1
k1=2
For i=1 To 7
s=s-dx*c(i) 'next term of the series
dx=dx*n1*(n+k1)*(n+k) 'next derivative of f(k)
k=k+2
k1=k1+2
Next
Function = s
End Function
function lRun() as long link #pRun
dim i as long
dim j as long
dim x as double
For i=1 To 10000
For j=2 To 50
x=Zeta(j)
Next
Next
end function
Sub finish() link #pFinish
terminate
End Sub
"
'---JIT compile O2 code
O2_Asm scr
If O2_Error Then
MsgBox 0,O2_Error
Stop
Else
O2_Exec
End If
'---Start thinBasic code
Declare Function Zeta(ByVal Double ) As Double At pZeta
Declare Function lRun() As Long At pRun
Declare Sub Finish() At pFinish
For j=2 To 20
x=Zeta(j)
Console_WriteLine j, x
Next
t=GetTickCount
lRun()
'For i=1 To 10000
' For j=2 To 50
' x=Zeta(j)
' Next
'Next
t=GetTickCount-t
Console_WriteLine "10000 iterations of computing the Zeta function from 2 to 50 in "&t&" mSec."
Console_WriteLine "All done. Press any key to finish"
Console_WaitKey
Finish()
Do you know where I can get a pre-compiled version (DLL) of GSL library?
Thanks a lot
Eros
Bookmarks