On Sep 7, 12:42 pm, "wang frank" <f...@hotmail.co.jpwrote:

Is my conclusion correct that Python is slower than matlab?

There are ways to speed up code execution, but to see substantial

improvement you'll need to use numpy and rework the code to operate

on vectors/matrices rather than building the result one step at the

time. This applies to Octave as well. See the example code at the end

of the message.

With that code computing 1 million logarithms showed the following

tendency

original =648.972728 msec per pass

optimized =492.613773 msec per pass

with numpy =120.578616 msec per pass

The "slowness of python in this example mainly comes from the function

call (math.log) as it seems about 30% of the runtime is spent calling

the function.

import timeit

setup = """

import math

from numpy import arange, log

size = 1000

"""

code1 = """

#original code

for i in range(size):

for j in range(size):

a = math.log(j+1)

"""

code2 = """

# minor improvements lead to 15% faster speed

from math import log

for i in xrange(size):

for j in xrange(size):

a = log(j+1)

"""

code3 = """

# applying via a universal function makes it 5 times faster

for i in xrange(size):

nums = arange( size )

a = log( nums + 1)

"""

N = 3

codes = [ code1, code2, code3 ]

for stmt in codes:

timer = timeit.Timer(stmt=stmt, setup=setup)

msec = 1000.0 * timer.timeit(number=N)/N

print "%f msec per pass" % msec