Skip to content

Update code.py to run faster#213

Closed
eduardopaula wants to merge 1 commit intobddicken:mainfrom
eduardopaula:patch-1
Closed

Update code.py to run faster#213
eduardopaula wants to merge 1 commit intobddicken:mainfrom
eduardopaula:patch-1

Conversation

@eduardopaula
Copy link

Changes Made:

  • Elimination of Nested Loops:
    Replaced the "for" loops with vectorized operations using NumPy, which are significantly faster.

  • Use of NumPy for Mathematical Operations:
    The modulus and summation operations were performed on a vector using NumPy ("np.arange" and ".sum()").

  • Efficient Array Creation:
    Used "np.full" to create the array initialized with the previously calculated sum value.

  • Vectorized Randomization and Summation:
    Added the random value "r" directly to the array "a" using vectorized operations.

@mmurrian
Copy link

mmurrian commented Dec 6, 2024

I wouldn't expect these changes to be merged given the authors intent with these benchmarks is to compare "the same" implementation of the algorithm. If Python struggles with for-loops, the benchmark is supposed to show that.

I should note that the C implementation of this benchmark, and every other language, is also limited by the way the algorithm is implemented. It's purposely inefficient.

If allowed to make equivalent optimizations to yours on the C benchmark, it would be another 40 times faster. So, this would make Python much faster. Applied to every other language, they would all end up much faster too.

@bddicken
Copy link
Owner

bddicken commented Dec 6, 2024

Thanks for submitting, however this significantly alters the amount of "work" the program is doing. We need to do ~ the same ~ amount of work in all programs for this to be reasonably fair.

@bddicken bddicken closed this Dec 6, 2024
@miraculixx miraculixx mentioned this pull request Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants