If you are comparing jit version to default version, there certainly will be a gap, especially during backward process. I noticed that both the implementation from here 3 and your implementation used +=[i], but I recommend using .append(i). I don’t know how are these two methods translated to C++ but it seems that .append(i) is faster than simply adding, especially when you want to add many elements.