Skip to content

Instantly share code, notes, and snippets.

@faroit
Last active April 18, 2016 11:22
Show Gist options
  • Save faroit/9c0aeb0efeb9b0154b156b4ff9aefded to your computer and use it in GitHub Desktop.
Save faroit/9c0aeb0efeb9b0154b156b4ff9aefded to your computer and use it in GitHub Desktop.

PYTHON

import numpy as np
import timeit

P = np.random.random((20, 15, 100, 30, 2))

A = np.random.random((20, 15, 100, 5))
H = np.random.random((30, 5))
C = np.random.random((2, 5))


def run():
    return np.einsum('abfj,tj,cj->abftc', A, H, C)

times = timeit.Timer(run).timeit(number=10)

print times

Elapsed time: 1.44563603401 seconds

Julia

using Einsum

P = zeros(20,15,100,30,2); 

A = randn(20,15,100,5);
H = randn(30,5);
C = randn(2,5);

tic()
for i = 1:10
  @einsum P[a,b,f,t,c] = A[a,b,f,j]*H[t,j]*C[c,j]
end
toc()

Elapsed time: 85.405141333 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment