Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update python code #245

Closed
wants to merge 1 commit into from

Conversation

miraculixx
Copy link

@miraculixx miraculixx commented Dec 13, 2024

I'm submitting this for a Python implementation that's both on par with C, using the original code in terms of code structure and logic (in particular, a nested for-loop), and is using only the Python standard library.

This is particularly addressing the concern raised in #213, #217 where you mention that the amount of (computation) work should be the same regardless of the language, and hence pre-computing u % j is not valid. Similarly, this is not using an external library (numpy), as you argued in #63 that only built-in / standard library features are allowed.

Approach

  • code.py is native python code, using the standard library; leveraging compute-parallelism and memoization
  • cythonized.py is compiled to c using Cython, a python compiler

Results

  • code.py runs at approx. 10x C speed (with memoization) to at 1/10x C speed (compute-parallel, without memoization). That's a 4x to 40x speed-up v.v. the original code.
  • cythonized.py runs at roughly C speed, that's a 30x speed-up v.v. the original code.

Rationale

  • memoization and compute-parallelism are commonly used built-in features of the Python standard library
  • Cython is an optimised static compiler for the Python programming language

One could argue that memoization reduces the amount of work done; however, this is up to the program itself, i.e. the work specified by the program is the same regardless (unlike code-specified optimization, such as pre-computation or explicit caching). In some sense, this is semantically equivalent to other languages' compiler optimizations and use of just in time compilation (JIT).

The use of Cython to compile the code to native code is the same approach used by other languages. The program used for this (cythonized.py) is otherwise identical with the original code.

- code.py is native python
- cythonized.py is compiled to c using cython, a python compiler


@cache
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a cache is "cheating" in this case. It allows the program to skip huge amounts of the work relative to other implementations. I mean, it's cool that python supports this, but a similar cache could be implemented in other languages giving them huge boosts also.

Copy link
Author

@miraculixx miraculixx Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, in this case all JIT and compiler optimizations should not be allowed either, because they are essentially "cheating" too, right? Otherwise the benchmark is comparing compilers, not languages.


def parallel(fn, iterable):
# helper for parallel processing as a transparent fallback in case memoization is not effective
from multiprocessing import Pool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi threading and multi processing are not allowed for this particular benchmark. Though, if you have an idea for a multi-process-specific benchmark, feel free to make a PR to add a new one!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thougt the objective is to benchmark 1 billion operations? I get that you want some ground rules, but why does it matter how that is achieved?

for j in range(100000): # 100k inner loop iterations, per outer loop iteration
a[i] += j % u # Simple sum
a[i] += r # Add a random value to each element in array
print(a[r]) # Print out a single element from the arra
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already merged a PR for cython?

Copy link
Author

@miraculixx miraculixx Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is PR #222, however that is treating Cython as a separate language (because it is using Cython-specific syntax). This PR however uses Cython's pure python mode, i.e. same syntax as original code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already merged a PR for cython?

Actually, PR #222 hasn't been merged yet :)

@PEZ
Copy link
Collaborator

PEZ commented Feb 9, 2025

Closing this for now. As stated above already, we're not allowing memoization, caching, parallellism, or anything like that in the current benchmarks. I'd be interested in a cython entry. There's a new runner and any contributions need to target that.

@PEZ PEZ closed this Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants