Row | taking MAX 10000 so that time difference can be shown ; accessing element row wise ; accessing element column wise ; Driver code ; Time taken by row major order ; Time taken by column major order
MAX = 1000 NEW_LINE from time import clock NEW_LINE arr = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def rowMajor ( ) : NEW_LINE INDENT global arr NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( MAX ) : NEW_LINE INDENT arr [ i ] [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT def colMajor ( ) : NEW_LINE INDENT global arr NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( MAX ) : NEW_LINE INDENT arr [ j ] [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT t = clock ( ) NEW_LINE rowMajor ( ) ; NEW_LINE t = clock ( ) - t NEW_LINE print ( " Row ▁ major ▁ access ▁ time ▁ : { : .2f } ▁ s " . format ( t ) ) NEW_LINE t = clock ( ) NEW_LINE colMajor ( ) NEW_LINE t = clock ( ) - t NEW_LINE print ( " Column ▁ major ▁ access ▁ time ▁ : { : .2f } ▁ s " . format ( t ) ) NEW_LINE DEDENT