Click here to Skip to main content
15,893,486 members

Same algorithm runs much slower in vb .net 3.5 than in matlab

DonkeyBo asked:

Open original thread
Hi guys,

I am trying to implement a neural network training algorithm in vb .net. I have found a open-source matlab code of this algorithm, so basically I am simply converting the matlab code to vb and adding some UIs.

Right now I have already finished the job (they are getting the same results if the same data was tested), but the program runs much much slower in vb than in matlab, like about 30 times! What I did is just to run a small function and to compare the running time.

Here are the codes of the function (VB .NET):
VB
Public Function CalculateError(ByVal input As Matrix, ByVal output As Matrix) As Double
        Dim result As Double
        result = 0
        Dim Nm, Nn, Ni, No As Integer
        Dim net As Double
        Nm = input.mRow
        Nn = CInt(parameters.value(2))
        Ni = CInt(parameters.value(4))
        No = CInt(parameters.value(5))

        For p As Integer = 1 To Nm
            Dim temp = New List(Of Double)
            'compute the first Ni entries of temp
            For i As Integer = 1 To Ni
                temp.Add(input.value(p, i))
            Next

            For n As Integer = 1 To Nn
                net = weights.value(iw.value(n))
                For i As Integer = iw.value(n) + 1 To iw.value(n + 1) - 1
                    'Make sure topology is a matrix with integer entries only
                    net += temp(topology.value(i) - 1) * weights.value(i)
                Next

                temp.Add(ActivationFunction(n, net))
                'temp.Add(0.0)
            Next



            For k As Integer = 1 To No
                result += Math.Pow(output.value(p, 1) - temp(Nn + Ni - No + 1 - 1), 2)
            Next

        Next


        ' SSE
        Return result
    End Function


and matlab:

function [err] = calculate_error(input,output, topo, weight,param, iw, gain, act)
err = 0;
for p = 1:size(input, 1)     % number of patterns
   temp(1:param(4)) = input(p,1:param(4));
   for n = 1:param(2) % number of neurons
      j = param(4) + n;
      net = weight(iw(n));
      for i = (iw(n)+1):(iw(n+1)-1)
         net = net + temp(topo(i))*weight(i);
      end;
      tic
      out=actFunc(n,net,act,gain);
      toc
      temp(j) = out;
   end;
   for k = 1:param(5)
      err = err + (output(p,k)-temp(param(2)+param(4)-param(5)+k))^2;                % calculate total error
   end;
end;


The Matrix is a custom class I defined and it is implemented using Array; ActivationFunction is another simple function (I will attach the code also); and other variables like iw, weights are some internal variables, I don't think they really affect the runnning time much.

The code for ActivationFunction is:

VB
Public Function ActivationFunction(ByVal n As Integer, ByVal net As Double) As Double
        'Dim result As Double
        Select Case activations.value(n)
            Case 0
                Return gain.value(n) * net           'linear neuron
            Case 1
                Return 1.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net))     'unipolar neuron
            Case 2
                Return 2.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net)) - 1.0     'bipolar neuron
            Case 3
                Return (gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net))     'unipolar elliot neuron
            Case 4
                Return (2.0 * gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net)) - 1     'bipolar elliot neuron
        End Select
    End Function


The problem I have now is that I tried to run these two CalculateError functions (one in vb. net and other in matlab), the vb code needs like 50 milliseconds, while the matlab code runs in 1.5 millisecond! BTW, even for the ActivationFunction (even simpler), matlab runs much faster.

Anyone can help me with this or any idea that why the VB code is that slow?

TIA.
Tags: Visual Basic, MatLab

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900