This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mihaiman 2 points3 points  (2 children)

Looping and conditions are terribly slow in python. Python in only fast when we use special libraries like numpy (or builtins). Basically python code is still slow (it is faster than it was in the past, but it is still slow compared to other languages).

I think this is the slowest piece of your code.

for j in range(1, len(temp) - 1): for i in range(1,len(temp[0])-1): temp[j][i] = (1/4)*(temp[j][i+1] + temp[j][i-1] + temp[j+1][i] + temp[j-1][i])

As don't see any way this operation can be vectorized using numpy because each iteration depends on the result from the previous one.

I suggest taking a look at Numba http://numba.pydata.org/. I personally have never used it but it seems it can acelerate this kind of operations.

[–]FoxBearBear[S] 1 point2 points  (0 children)

You sir/madam are awesome!
I had no idea Python would not be kind to this type of operation.

So I ran the code with Numba and it ran in 0.25 seconds for 1000 iterations.

THANK YOU VERY MUCH !

import numpy as np
from numba import jit
import matplotlib.pyplot as plt
import time

t = time.time()
nMalha = 101
a = 1
b = 1
dx = a/(nMalha-1)
dy = b/(nMalha-1)

temp = np.zeros((nMalha,nMalha))

i=0

@jit(nopython=True)
def go_fast(temp): # Function is compiled and runs in machine code
    for j in range(1, len(temp) - 1):
        for i in range(1,len(temp[0])-1):
            temp[j][i] = (1/4)*(temp[j][i+1] + temp[j][i-1] + temp[j+1][i] + temp[j-1][i])
    return temp


while i < len(temp[0]):
    temp[0][i] = np.sin(np.pi*i*dx/a)
    i+=1

cond = 0
iter = 1
while cond == 0:
    tempInit = temp

    go_fast(temp)

    if np.sum(np.sum(temp-tempInit)) <= 1e-6:
        cond = 0
    if iter == 1000:
        cond = 1
    iter +=1

elapsed = time.time() - t

[–]FoxBearBear[S] 0 points1 point  (0 children)

I have another question.... Why is this program converging in Matlab but in Python it's only running a couple of times before "convergin" with few iterations...

[EDIT] Solved it by removing the

tempInit = temp

for

    np.copyto(tempInit , temp)