# Prange slowdown

**URL:** <https://numba.discourse.group/t/prange-slowdown/1316>\
**Category:** Support: How do I do ...?\
**Created:** [April 21, 2022, 1:57pm UTC](https://numba.discourse.group/t/prange-slowdown/1316 "2022-04-21T13:57:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReaperX](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/reaperx/32/584_2.png) [@ReaperX](https://numba.discourse.group/u/ReaperX)\
**Post date:** [April 21, 2022, 1:57pm UTC](https://numba.discourse.group/t/prange-slowdown/1316/1 "2022-04-21T13:57:42Z")

</div>

The following function is part of a program to create renderings of the buddhabrot:

```auto
@njit(parallel=False, fastmath=True)  
def buddhabrot_trajectory(c, point_list, iteration_ranges, iteration_ranges_of_escape, iterations_for_escape):                      
    for o in prange(0,OVERSAMPLE):        
        if not definitely_in_mandelbrot_set(c[o]):
            iteration_range_counter = 0
            while (iteration_range_counter<NUM_ITER_RANGES):
                k = iterate_and_collect(c[o], iteration_ranges[iteration_range_counter], iteration_ranges[iteration_range_counter+1], point_list[:,o])
                if k < iteration_ranges[iteration_range_counter+1]:
                    iteration_ranges_of_escape[o] = iteration_range_counter
                    iteration_range_counter = NUM_ITER_RANGES
                    iterations_for_escape[o] = k
                else:
                    iteration_range_counter += 1
    return

```

In this, c is a list of (slightly different) complex numbers of length OVERSAMPLE. For each of these, a function iterate\_and\_collect is called that performs the Mandelbrot iteration and collects all points created in a separate slice of point\_list.

iteration\_ranges\_of\_escape and iterations\_for\_escape are numpy arrays of length OVERSAMPLE and dtype=np.uint32.

My thinking was that by creating these numpy arrays outside the function and reusing them, I would save the overhead of creating local arrays inside the function.

I wrote this function with the idea that the prange(0,OVERSAMPLE) loop would be parallelizable. The calculations for each o in the loop are independent, and I set up the arrays so that no race conditions would occur.

Still, parallel=True causes a dramatic slowdown of this function rather than the expected speedup. I roughly timed factor 6 slower. CPU load is above 90% with parallel=True (vs 10% with parallel=False), so something is happening, just not the huge performance increase I am trying to get.

I have read [Automatic parallelization with @jit — Numba 0.50.1 documentation](https://numba.pydata.org/numba-doc/latest/user/parallel.html) but I have not found a solution. I’m aware that there is a problem when the same variable or slice is being written to by parallel threads, but I thought I had taken care to avoid that in my code.
