I want to create a histogram (1d) from given ranges and I want it to be fast. This is why also cython and numba can be taken into consideration. Given are two boundaries, weights and a stepsize. For example: boundaries1 = np.array([511.4, 517.5, 517.2, 520.1, 519.8]) boundaries2 = np.array([517.3, 517.1, 517.8, 518.2, 515.0]) weights = np.array([0.2, 0.4, 0.6, 0.8, 1]) stepsize = 0.1 Note, that the boundaries are not sorted by their value, meaning that e.g. a boundary1 could be greater or lower as the corresponding boundary2. The result for this example should return a histogram with the given stepsize, the minimum value and the maximum value. histogram = np.array([0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 1.6, 2.2, 2.2, 2. , 2. , 1.6, 1.6, 1.6, 1. , 1. , 1. , 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 1.8, 0.8, 0.8, 0.8]) minimum = 511.4 maximum = 520.1 Are there creative ideas on how to implement this to run as fast as possible? This is my current approach: @njit(cache=True) def create_histogram(boundaries1, boundaries2, weights, stepsize): minimum = np.minimum(np.min(boundaries1),np.min(boundaries2)) maximum = np.maximum(np.max(boundaries1),np.max(boundaries2)) num_bins = int(np.round((maximum-minimum+stepsize)/stepsize)) hist = np.zeros(num_bins, dtype=np.float64) lower = np.round((np.minimum(boundaries1, boundaries2)-minimum)/stepsize).astype(np.int32) upper = np.round((np.maximum(boundaries1, boundaries2)-minimum)/stepsize).astype(np.int32) for i in range(lower.shape[0]): hist[lower:upper+1] += weights return hist, minimum, maximum Continue reading...