# Parallel slower than non parallel

Howdy. I just made my first attempt at parallel computing in Sage. It didn't go well: My parallel version of the program is slower than the non-parallel version. I assume I've done something wrong. How can I fix this, or at least diagnose the problem? Note that the input (M) is a matrix.

Here's the parallel version:

@parallel
def totaler(i, M_4, M_2, M):
return M_4[i][i] - sum(M_2[i]) - (sum(M[i]) ** 2 - sum(M[i]))
def bccounter(M):
M_2 = M ** 2
M_4 = M_2 ** 2
n = len(M)
total = sum([x for x in totaler([(y, M_4, M_2, M) for y in range(n)])])
return total / 8


Here's the non-parallel version:

def bccounter(M):
M_2 = M ** 2
M_4 = M_2 ** 2
n = len(M)
total = 0
for i in range(n):
total += M_4[i][i] - sum(M_2[i]) - (sum(M[i]) ** 2 - sum(M[i]))
return total / 8


If you're curious, it counts the number of 4-cycles in a graph with adjacency matrix M.

edit retag close merge delete

## Comments

1

The @parallel decorator works by creating a separate process for each computation (each call of totaler, in your case). So if the time it takes to do the computation is less than the time it takes to start a new process, the parallel computation will definitely be slower. How much time does a typical call to totaler take?