I am learning how to use Numba (while I am already fairly familiar with Cython). How should I go about speeding up this code? Notice the function returns a dict of sets of two-tuples of ints. I am using IPython notebook. I would prefer Numba over Cython.
@autojit def generateadj(width,height): adj = {} for y in range(height): for x in range(width): s = set() if x>0: s.add((x-1,y)) if x<width-1: s.add((x+1,y)) if y>0: s.add((x,y-1)) if y<height-1: s.add((x,y+1)) adj[x,y] = s return adj
I managed to write this in Cython but I had to give up on the way data is structured. I do not like this. I read somewhere in Numba documentation that it can work with basic things like lists, tuples, etc.
%%cython import numpy as np def generateadj(int width, int height): cdef int[:,:,:,:] adj = np.zeros((width,height,4,2), np.int32) cdef int count for y in range(height): for x in range(width): count = 0 if x>0: adj[x,y,count,0] = x-1 adj[x,y,count,1] = y count += 1 if x<width-1: adj[x,y,count,0] = x+1 adj[x,y,count,1] = y count += 1 if y>0: adj[x,y,count,0] = x adj[x,y,count,1] = y-1 count += 1 if y<height-1: adj[x,y,count,0] = x adj[x,y,count,1] = y+1 count += 1 for i in range(count,4): adj[x,y,i] = adj[x,y,0] return adj