Python icon indicating copy to clipboard operation
Python copied to clipboard

optimize split_matrix function

Open ivanz851 opened this issue 11 months ago • 4 comments

I optimized split_matrix function by removing duplicate code to the extract_submatrix function, and added tests to test_strassen_matrix_multiplication.py file

ivanz851 avatar Jan 31 '25 20:01 ivanz851

This increases readability and maintainability of the code, but certainly does not "optimize" the algorithm, for you have added the extra overhead of calling a function, which will actually slow down the algorithm.

josmithua avatar Feb 03 '25 08:02 josmithua

This increases readability and maintainability of the code, but certainly does not "optimize" the algorithm, for you have added the extra overhead of calling a function, which will actually slow down the algorithm.

Actually, my code with extract_submatrix function is not less efficient than the original one, since there is exactly same number of conditional jumps. This can be checked using godbolt.org: comparison. If you follow the link, you can see disassembly of my revision of the code on the left and of the original code on the right. In original code, there are 8 FOR_ITER jumps and 8 JUMP_BACKWARD jumps. In new code, there are 2 FOR_ITER jumps and 2 JUMP_BACKWARD jumps in extract_submatrix function, which is called 4 times, so in sum we have same 8 FOR_ITER jumps and 8 JUMP_BACKWARD jumps. That's why I think code with extract_submatrix function is not slower and can be used for better readability.

ivanz851 avatar Feb 03 '25 15:02 ivanz851

@josmithua

ivanz851 avatar Feb 03 '25 15:02 ivanz851

@ivanz851 Thanks, I learned something new today. Cheers

josmithua avatar Feb 03 '25 16:02 josmithua