<jupyter_start><jupyter_text>calc diffusion problem<jupyter_code>grid_shape=(1024,1024)
D=1
dx,dy,dt=(1,1,0.1)

def laplacian(grid):
    return (np.roll(grid,+1,0)+np.roll(grid,-1,0) -2*grid)/(dx*dx)+(np.roll(grid,+1,1)+np.roll(grid,-1,1)-2*grid)/(dy*dy)

def evolve2d(grid):
    return grid + dt * D * laplacian(grid)

def run_experiment(num_iterations):
    grid=np.zeros(grid_shape)
    block_low = int(grid_shape[0] * 0.4)
    block_high= int(grid_shape[0] * 0.5)
    grid[block_low:block_high,block_low:block_high]=0.005
    
    start=time.time()
    for i in range(num_iterations):
        grid=evolve2d(grid)
    end=time.time()
    
    return end-start
%timeit run_experiment(10)<jupyter_output>1 loop, best of 3: 789 ms per loop<jupyter_text>refactoring<jupyter_code>grid_shape=(10,10)
D=1
dx,dy,dt=(0.1,0.1,0.1)

def laplacian_refactor(grid,out):
    np.copyto(out,grid)
    out *=-4
    out += np.roll(grid,+1,0)
    out += np.roll(grid, -1,0)
    out += np.roll(grid,+1,1)
    out += np.roll(grid, -1,1)

def evolve2d_refactor(grid,out):
    laplacian_refactor(grid,out)
    out*=D*dt
    out+=grid
    
def run_experiment_refactor(num_iterations):
    grid           = np.zeros(grid_shape)
    grid_next = np.zeros(grid_shape)
    block_low = int(grid_shape[0] * 0.4)
    block_high= int(grid_shape[0] * 0.5)
    grid[block_low:block_high,block_low:block_high]=0.005
    
    start=time.time()
    for i in range(num_iterations):
        evolve2d_refactor(grid,grid_next)
        grid,grid_next=grid_next,grid
    end=time.time()
    
    return end-start
%timeit run_experiment_refactor(10)<jupyter_output>The slowest run took 8.94 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 286 µs per loop
{'fraction_non_alphanumeric': 0.14454443194600675, 'fraction_numerical': 0.048931383577052866, 'mean_word_length': 4.0539772727272725, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '223524', 'license': 'MIT', 'n_tokens_mistral': 772, 'n_tokens_neox': 720, 'n_words': 121, 'path': 'diffusionEq/diffusion-problem.ipynb', 'repo_name': 'terasakisatoshi/pythonCodes', 'size': '1778'}