Eigentlich bin ich ja ein grosser Fan von VB.net... nach dem hier nicht mehr so:
c# code:
vb.net:
Kommt vom Computer Language shootout. Probierts mal aus! Bei mir ist C# echt etwa 50 % schneller!! EDIT: Sorry, sogar 100%! mit n==1000 komme ich bei c# auf 703 MS, während es vb.net in lächerlichen 1530 ms schafft. Das ist grauenhaft!
c# code:
Code:
using System;
class SpectralNorm
{
public static void Main(String[] args) {
int n = 5000;
if (args.Length > 0) n = Int32.Parse(args[0]);
Console.WriteLine("{0:f9}", new SpectralNorm().Approximate(n));
}
double Approximate(int n) {
// create unit vector
double[] u = new double[5000];
for (int i=0; i<n; i++) u[i] = 1;
// 20 steps of the power method
double[] v = new double[5000];
for (int i=0; i<n; i++) v[i] = 0;
for (int i=0; i<10; i++) {
MultiplyAtAv(ref n, ref u, ref v);
MultiplyAtAv(ref n, ref v, ref u);
}
// B=AtA A multiplied by A transposed
// v.Bv /(v.v) eigenvalue of v
double vBv = 0, vv = 0;
for (int i=0; i<n; i++) {
vBv += u[i]*v[i];
vv += v[i]*v[i];
}
return Math.Sqrt(vBv/vv);
}
/* return element i,j of infinite matrix A */
double A(ref int i, ref int j){
return 1.0/((i+j)*(i+j+1)/2 +i+1);
}
/* multiply vector v by matrix A */
void MultiplyAv(ref int n, ref double[] v, ref double[] Av){
for (int i=0; i<n; i++){
Av[i] = 0;
for (int j=0; j<n; j++) Av[i] += A(ref i, ref j)*v[j];
}
}
/* multiply vector v by matrix A transposed */
void MultiplyAtv(ref int n, ref double[] v, ref double[] Atv){
for (int i=0;i<n;i++){
Atv[i] = 0;
for (int j=0; j<n; j++) Atv[i] += A(ref j,ref i)*v[j];
}
}
/* multiply vector v by matrix A and then by matrix A transposed */
void MultiplyAtAv(ref int n, ref double[] v, ref double[] AtAv){
double[] u = new double[5000];
MultiplyAv(ref n, ref v, ref u);
MultiplyAtv(ref n, ref u, ref AtAv);
}
}
vb.net:
Code:
Option Strict On
Imports System
Class SpectralNorm
Public Shared Sub Main(ByVal args As String())
Dim n As Integer = 5000
If args.Length > 0 Then
n = Int32.Parse(args(0))
End If
Console.WriteLine("{0:f9}", New SpectralNorm().Approximate(n))
End Sub
Private Function Approximate(ByVal n As Integer) As Double
' create unit vector
dim u(5000) as Double
For i As Integer = 0 To n - 1
u(i) = 1
Next
' 20 steps of the power method
dim v(5000) as Double
For i As Integer = 0 To n - 1
v(i) = 0
Next
For i As Integer = 0 To 9
MultiplyAtAv(n, u, v)
MultiplyAtAv(n, v, u)
Next
' B=AtA A multiplied by A transposed
' v.Bv /(v.v) eigenvalue of v
Dim vBv As Double = 0, vv As Double = 0
For i As Integer = 0 To n - 1
vBv += u(i) * v(i)
vv += v(i) * v(i)
Next
Return Math.Sqrt(vBv / vv)
End Function
' return element i,j of infinite matrix A
Private Function A(ByRef i As Integer, ByRef j As Integer) As Double
Return 1 / ((i + j) * (i + j + 1) / 2 + i + 1)
End Function
' multiply vector v by matrix A
Private Sub MultiplyAv(ByRef n As Integer, ByRef v As Double(), ByRef Av As Double())
For i As Integer = 0 To n - 1
Av(i) = 0
For j As Integer = 0 To n - 1
Av(i) += A(i, j) * v(j)
Next
Next
End Sub
' multiply vector v by matrix A transposed
Private Sub MultiplyAtv(ByRef n As Integer, ByRef v As Double(), ByRef Atv As Double())
For i As Integer = 0 To n - 1
Atv(i) = 0
For j As Integer = 0 To n - 1
Atv(i) += A(j, i) * v(j)
Next
Next
End Sub
' multiply vector v by matrix A and then by matrix A transposed
Private Sub MultiplyAtAv(ByRef n As Integer, ByRef v As Double(), ByRef AtAv As Double())
dim u(5000) as Double
MultiplyAv(n, v, u)
MultiplyAtv(n, u, AtAv)
End Sub
End Class
Kommt vom Computer Language shootout. Probierts mal aus! Bei mir ist C# echt etwa 50 % schneller!! EDIT: Sorry, sogar 100%! mit n==1000 komme ich bei c# auf 703 MS, während es vb.net in lächerlichen 1530 ms schafft. Das ist grauenhaft!
Zuletzt bearbeitet: