Scaling arrays

I would like to know if there is a more rapid way to multiply an array with a number in Visual Basic than the one presented below. I haven't found much on vectors in the help and seems that the only possible way would be to use the matrix class.  

e.g.  {1,3,4}*2 = {2,6,8}

I know this way to do that looping through the array:

Dim a() As Integer = {1, 2, 3, 4}

Dim b As Integer = 2

Dim c As Integer

For c = 0 To 3

a(c) = a(c) * b

Next

however the thing doesn't really work well for large arrays (in term of time), therefore I was wondering if the same operation can be performaed differently.  For example using a vector in a way that with a single operation I can multiply all the values of an array at once.

Thanks

 



Answer this question

Scaling arrays

  • ke4vtw

    hi,

    i didn't understand your question but sounds you talk about the array bounds, to declare an array you have to know its length, if you don't know the length you can use ArrayList or Generics

    i didn't use Generics b4 but ArrayList has ToArray method you can turn it to normal array search MSDN for it

    hope this helps



  • hashi

  • menardgi

    My question is different- sorry if I wasn't clear enough.

    I have added an example

    Thanks


  • sasseen

    hi,

    i don't think this will take a long time i did that in an array has 10 million item and it took 400 millisecond



    Dim array() As Integer = New Integer(10000000) {}
    Dim As Integer = 0
    Do While (i < array.Length)
        
    array(i) i
        i 
    (i + 1)
    Loop
    Dim 
    n1 As DateTime DateTime.Now
    Dim 
    As Integer = 0
    Do While (i < array.Length)
        
    array(i) (array(i) * 2)
        i 
    (i + 1)
    Loop
    Dim 
    n2 As DateTime DateTime.Now
    Dim 
    As TimeSpan (n2 - n1)
    Console.WriteLine(s.Milliseconds.ToString)
    Dim As Integer = 0
    Do While (i < 20)
        Console.WriteLine(
    array(i).ToString)
        i 
    (i + 1)
    Loop

     

    but when i tried this with arraylist  (its a collection like array but doesn't has bounds ) i created a class called it vector inherits from ArrayList class , and i added a  method for multiply . it worked, but it took much more time



    Namespace ConsoleApplication1
        
        
        
    Class Program
            
            
    Private Shared Sub Main(ByVal args() As String)
                
    Dim array() As Integer = New Integer((1000000) - 1) {}
                
    Dim As Integer = 0
                
    Do While (i < array.Length)
                    
    array(i) i
                    i 
    (i + 1)
                
    Loop
                Dim 
    itms As vector New vector
                itms.AddRange(
    array)
                
    Dim n1 As DateTime DateTime.Now
                
    itms.Multiply(2)
                
    Dim n2 As DateTime DateTime.Now
                Dim 
    As TimeSpan (n2 - n1)
                Console.WriteLine(s.Milliseconds.ToString)
                
    Dim As Integer = 0
                
    Do While (i < 50)
                    Console.WriteLine(itms(i).ToString)
                    i 
    (i + 1)
                
    Loop
            End Sub
        End Class
        
        Class 
    vector
            
    Inherits ArrayList
            
            
    Public Sub Multiply(ByVal by As Integer)
                
    Dim As Integer = 0
                
    Do While (i  _
                            < (
    Me.Count - 1))
                    
    Me(i) (CType(Me(i),Integer) * 2)
                    i 
    (i + 1)
                
    Loop
            End Sub
        End Class
    End Namespace


     

    hope this helps



  • Todd Virlee

    Thanks very much for the extensive help

    I have tried the first one and it seems to be working better

    Cheers


  • Don Demsak XML MVP

    you are welcome

  • Scaling arrays