Is a .NET array slower than a list in IronPython?

I did the following code-based matrix multiplication test in IronPython here :

from System import Random
from System.Diagnostics import Stopwatch

def zero(m,n):
    # Create zero matrix
    new_matrix = [[0 for row in range(n)] for col in range(m)]
    return new_matrix

def rand(m,n):
    # Create random matrix
    rnd = Random(1)
    new_matrix = [[rnd.NextDouble() for row in range(n)] for col in range(m)]
    return new_matrix

def show(matrix):
    # Print out matrix
    for col in matrix:
        print col 

def mult(matrix1,matrix2):
    # Matrix multiplication
    if len(matrix1[0]) != len(matrix2):
        # Check matrix dimensions
        print 'Matrices must be m*n and n*p to multiply!'
    else:
        # Multiply if correct dimensions
        watch = Stopwatch()
        print 'mult1 start....'
        watch.Start()
        new_matrix = zero(len(matrix1),len(matrix2[0]))
        for i in range(len(matrix1)):
            for j in range(len(matrix2[0])):
                for k in range(len(matrix2)):
                    new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
        watch.Stop()
        print 'mult1 end.'
        print watch.ElapsedMilliseconds
        return new_matrix

from System import Array

def ListToArray(matrix):
    n = len(matrix)
    m = len(matrix[0])
    a = Array.CreateInstance(float, n, m)
    for i in range(n):
        for j in range(m):
            a[i,j] = matrix[i][j]
    return a


def mult2(matrix1, matrix2):

    N = len(matrix1)
    K = len(matrix2)
    M = len(matrix2[0])

    m1 = ListToArray(matrix1)
    m2 = ListToArray(matrix2)
    res = ListToArray(rand(len(matrix1), len(matrix2[0])))

    watch = Stopwatch()
    print 'mult2 start...'
    watch.Start()
    for i in range(N):
        for j in range(M):
            for k in range(K):
                res[i,j] += m1[i,k]*m2[k,j]
    watch.Stop()
    print 'mult2 ends.'
    print watch.ElapsedMilliseconds
    return res


if __name__ == '__main__':
    #a = rand(280,10304)
    #b = rand(10304,280)

    a = rand(280,10)
    b = rand(10,280)

    c = mult2(a, b)
    d = mult(a, b)

I want to try two large matrices (280 by 10304 and 10304 by 208), but both versions cannot produce results in a short time.

Then I tried a lot less (as shown in the code), I get the following result:

mult2 : 7902 ms
mult1 : 420 ms

indicates that using a .NET array in IronPython is much slower than a python list.

Also note that C # uses about ~ 12 seconds for two large matrices. IronPython already spends a lot of 10K times less. I'm not sure if the IronPython setting on my computer is incorrect or not, if not, then IronPython is very slow for numeric code.

+3
source share
4

, , - IronPython ( ) , . CLR Array , , , IronPython , , . # , IronPython .

, NumPy IronPython .

+2

.Net, . , Array , python , , numpy.

+1

, . .net, http://numerics.mathdotnet.com/ - , .

, , , .

0

My guess is that killing performance over regular Python lists is using a multidimensional array. I could easily notice that this is a slow way, and if you switched to arrays of arrays or figured out the index yourself, you could see more speed.

Others have noted that this is not the best code for a dynamic language such as Python. But one of IronPython's biggest benefits is how easily and without problems C # fragments can be used to speed up such cases.

0
source

All Articles