Yesterday I posted What is the best way / algorithm to make this sequential sorting code more performant (deleted) and it was closed with needs to be more focused mark (It had a mistake and was not clear enough). I posted a new question Is it possible to improve the performance of this code but with several edits based on advises in Why was my question about improving the performance of sequential sorting code in NumPy closed as lacking focus? : shortening the code, explaining the goal again in an understandable way, more focused and removed suspicious parts relating to probable multiple answers, but it closed again. The new post was:
Title: Is it possible to improve the performance of this code
I have a 2D array mod_arr with shape (
m*n
) that contains integers/indices and need to be modified (values in each row will be swapped in that row) based on some comparisons (sorting schemes) on values in the 3rd dimension (r
) of a 3D NumPy array with shape (m*n*r
) nav_org; In the 3D array the inner 2D arrays are considered independent of each other in comparisons. Rows will be swapped in each of these inner 2D arrays based on some sequential comparisons for the columns’ values. So, I first modify the 3D array and then based on the changes that were applied on this array, I modify the 2D integer array. So, in each of the inner 2D arrays:
- Sorting based on the 4th column in reverse order
- Sorting the first 7 rows based on the 2nd column in reverse order (Such separations are for comparing independently i.e., independent from values in the rest 3 rows, where nums_div = 10 --> comparison will be among just these 7 rows)
- Sorting the rest 3 rows based on the 1st column
CODES
As this code will be called many times in my simulation, it will be better if could be more performant (now, for
nums=1000000
,params=4
,nums_div=10
, it takes around 0.8 S, where 11 hours will be consumed by that if we call just the body that shown on the code 50000 times by a system with corei5 CPU and memory 16 Gb). I am seeking for any other faster intelligent way if possible, without Cython, with NumPy or Numba, to get res (np.int64
), wherenav
and comparations must be based on at leastnp.float64
(or more precise if it could, not the main goal; any recommendation will be appreciated in this regard).
Notes:
- For params and nums_div, the mentioned values (3 and 7 respectively) are almost used, so consider them instead the minimum values for benchmarks.
- The proposed code, preferably, be applicable on the both Python versions 2 and 3; Python 3 is the first priority.
- Be careful about choosing
nums
andnums_div
which must satisfynums % nums_div = 0
for data creation in the prepared example.
I couldn't figure out the main issue with that to improve that or post a new question in the true manner.
Please help further to clarify the issues with my post.