Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,行列演算において、numpy使用、不使用の処理時間の差を、調べてみた

Python(パイソン) プログラム作ってみた インデックス へ

2024.8.18 presented in [note] ( //note.com/runningWater/ )
2024.8.24 rewritten

----------
1 はじめに

これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。

以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。

しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。

その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。

このプログラムは、Python(パイソン) 言語を使って、記述されている。

----------
2 どんなものを作ったのか
 
2個の行列の積となる行列を作る、というような時には、[numpy]を使わないでやる、のと、[numpy]を使ってやる、のと、2通りの方法がある、と、聞いていた。どちらの方法を使うかによって、処理時間も、異なってくる、という事も、聞いていた。

そこで、それを確かめてみようと、思い、プログラムを作った。

----------
3 プログラムの内容

下記のような、1個のモジュールを、作ってみた。

===============
ファイル名 [MatrixTest.py]

----------

import numpy as NUMPY
import time

global_matrix_NUMPY_mult_A_B = " "
global_matrix_NotUsingNUMPY_A_B = " "
global_process_start_time = " "
global_process_end_time = " "

       # --------------------------
def matrix_multuply_test_1 ( arg_test_mode
                 , arg_matrix_A
                 , arg_matrix_NUMPY_A
                 , arg_matrix_B
                 , arg_matrix_NUMPY_B
                 ) :

    print ( "---------- matrix_multuply_test_1  -----" )

    global global_process_start_time
    global global_process_end_time

    global_process_start_time = time.time ( )

    process_counter = 0
    process_continue = "Y"
    while ( process_continue == "Y" ) :
        do_matrix_multiply_operation ( arg_test_mode
                 , arg_matrix_A
                 , arg_matrix_NUMPY_A
                 , arg_matrix_B
                 , arg_matrix_NUMPY_B
                                      )
        process_counter += 1

        #print ( "---------- matrix_multuply_test_1  -----" )
        #print ( "process_counter = " , process_counter )

        if ( process_counter > 80000 ) :
            process_continue = "N"
            break


    global_process_end_time =  time.time ( )

    return

       # -------------------------------
def  do_matrix_multiply_operation ( arg_test_mode
                         , arg_matrix_A
                         , arg_matrix_NUMPY_A

                         , arg_matrix_B
                         , arg_matrix_NUMPY_B
                              ) :

    global global_matrix_NUMPY_mult_A_B
    global global_matrix_NotUsingNUMPY_A_B

               #multiply tow Matrix
    if ( arg_test_mode == "U") :
               # use numpy mode
        global_matrix_NUMPY_mult_A_B = NUMPY.matmul (  \
                             arg_matrix_NUMPY_A
                           , arg_matrix_NUMPY_B )
    else :
                # not use numpy mode
        global_matrix_NotUsingNUMPY_A_B =\
            matrix_NotUsingNUMPY_multiply  ( \
                            arg_matrix_A
                          , arg_matrix_B )

    return

       # -------- not use numpy ---------
def matrix_NotUsingNUMPY_multiply  ( \
                            arg_matrix_A
                          , arg_matrix_B )  :

    #  print ( "---------- matrix_NotUsingNUMPY_multiply  -----" )

    return_matrix = [ [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]

    for set_row in range ( 0 , 3 ) :
        for set_column in range ( 0 , 3 ) :

            get_row_from_A = set_row
            get_column_from_B = set_column

            for get_round in range ( 0 , 3 ) :

                get_column_from_A = get_round
                get_row_from_B = get_round

                get_value_from_A = \
                  matrix_A [ get_row_from_A ]  [ get_column_from_A ]
                get_value_from_B = \
                  matrix_B [ get_row_from_B ] [ get_column_from_B ]

                w = get_value_from_A * get_value_from_B
                return_matrix [ set_row ] [ set_column ]  \
                   += w

    return  return_matrix

   #-----------------------------------------
print ( "NUMPY.__version__ = " , NUMPY.__version__ )

matrix_A =  [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ]
matrix_B =  [ [ 6 , 7 , 8 ] , [ 3 , 4 , 5 ] , [ 0 , 1 , 2 ] ]

matrix_NUMPY_A = NUMPY.array ( matrix_A )
matrix_NUMPY_B = NUMPY.array ( matrix_B )

print ( "==================================" )
print ( "matrix_A = " )
print ( matrix_A )
print ( "-------------------------" )
print ( "matrix_NUMPY_A = " )
print ( matrix_NUMPY_A )

print ( "==================================" )
print ( "matrix_B = " )
print ( matrix_B )
print ( "-------------------------" )
print ( "matrix_NUMPY_B = " )
print ( matrix_NUMPY_B )

    #*********************
    # mode : use numpy ( "U" ) or not ( "N" )
#test_mode = "N"
test_mode = "U"
    #*********************

matrix_multuply_test_1 ( test_mode
                 , matrix_A
                 , matrix_NUMPY_A
                 , matrix_B
                 , matrix_NUMPY_B
                          )

print ( "===============================" )
print ( "test_mode = " + test_mode )

if ( test_mode == "U" ) :
    print ( "global_matrix_NUMPY_mult_A_B = " )
    print ( global_matrix_NUMPY_mult_A_B )
else :
    print ( "global_matrix_NotUsingNUMPY_A_B = " )
    print ( global_matrix_NotUsingNUMPY_A_B )

process_needed_time = global_process_end_time - global_process_start_time

#print ( "global_process_start_time = " , global_process_start_time )
#print ( "global_process_end_time = " , global_process_end_time )
print ( "process_needed_time = " , process_needed_time )

print ( "===============================" )


 

----------

このモジュールを起動してみたら、以下のように、表示された。

NUMPY.__version__ =  2.0.1
==================================
matrix_A = 
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
-------------------------
matrix_NUMPY_A = 
[[0 1 2]
 [3 4 5]
 [6 7 8]]
==================================
matrix_B = 
[[6, 7, 8], [3, 4, 5], [0, 1, 2]]
-------------------------
matrix_NUMPY_B = 
[[6 7 8]
 [3 4 5]
 [0 1 2]]
---------- matrix_multuply_test_1  -----
===============================
test_mode = U
global_matrix_NUMPY_mult_A_B = 
[[ 3  6  9]
 [30 42 54]
 [57 78 99]]
process_needed_time =  0.10715842247009277
===============================

[numpy]を使用して、行列の積を求めた時の、処理時間は、上記のようになった。

----------

次に、プログラムの下記箇所を、このように変えてから、プログラムを起動してみた。

    #*********************
    # mode : use numpy ( "U" ) or not ( "N" )
test_mode = "N"
#test_mode = "U"
    #*********************

----------

NUMPY.__version__ =  2.0.1
==================================
matrix_A = 
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
-------------------------
matrix_NUMPY_A = 
[[0 1 2]
 [3 4 5]
 [6 7 8]]
==================================
matrix_B = 
[[6, 7, 8], [3, 4, 5], [0, 1, 2]]
-------------------------
matrix_NUMPY_B = 
[[6 7 8]
 [3 4 5]
 [0 1 2]]
---------- matrix_multuply_test_1  -----
===============================
test_mode = N
global_matrix_NotUsingNUMPY_A_B = 
[[3, 6, 9], [30, 42, 54], [57, 78, 99]]
process_needed_time =  0.4074821472167969
===============================

----------

[numpy]を使用しないで、行列の積を求めた時の、処理時間は、上記のようになった。

この結果からは、[numpy]を使用して、行列の積を求める、というようにした方が、処理時間の点で有利である、ということが、いえるようだ。

ただし、これについては、[Python]や[numpy]のバージョン、あるいは、動作環境の違いによっても、結果が変わってくるかもしれない。

----------  

Python(パイソン) プログラム作ってみた インデックス へ

この記事が気に入ったらサポートをしてみませんか?