-
Notifications
You must be signed in to change notification settings - Fork 335
update python code #245
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import sys | ||
| from functools import cache | ||
|
|
||
|
|
||
| @cache | ||
| def fibonacci(n): | ||
| if n == 0: | ||
| return 0 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,28 @@ | ||
| from functools import cache | ||
| import sys | ||
| import random | ||
|
|
||
| @cache | ||
| def compute(a, u, r): | ||
| # memoized function - don't recompute if the same values are passed | ||
| for j in range(100000): # 100k inner loop iterations, per outer loop iteration (for same values a, u, r) | ||
| a += j % u # Simple sum | ||
| return a + r # Add a random value to each element in array | ||
|
|
||
| def main(): | ||
| u = int(sys.argv[1]) # Get an input number from the command line | ||
| r = random.randint(0, 10000) # Get a random number 0 <= r < 10k | ||
| a = [0] * 10000 # Array of 10k elements initialized to 0 | ||
| for i in range(10000): # 10k outer loop iterations | ||
| 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 array | ||
| u = int(sys.argv[1]) # Get an input number from the command line | ||
| r = random.randint(0, 10000) # Get a random number 0 <= r < 10k | ||
| a = [0] * 10000 # Create an array of 10k elements | ||
| # 10k outer loop iterations, in parallel and memoized | ||
| tasks = ((a[i], u, r) for i in range(10000)) | ||
| a = list(parallel(compute, tasks)) | ||
| print(a[r]) # Print out a single element from the arra | ||
|
|
||
| def parallel(fn, iterable): | ||
| # helper for parallel processing as a transparent fallback in case memoization is not effective | ||
| from multiprocessing import Pool | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| with Pool() as p: | ||
| return p.starmap(fn, iterable) | ||
|
|
||
| main() | ||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import sys | ||
| import random | ||
| import cython | ||
|
|
||
| # cython.boundscheck(False) disables bounds checking for array access | ||
| # cython.cdivision(True) disables validity checking for division by zero | ||
| # cython.locals() declares the types of variables as native | ||
| @cython.boundscheck(False) | ||
| @cython.cdivision(True) | ||
| @cython.locals(u=cython.int, r=cython.int, a=cython.int[10000], j=cython.int, i=cython.int) | ||
| def main(): | ||
| u = int(sys.argv[1]) # Get an input number from the command line | ||
| r = random.randint(0, 10000) # Get a random number 0 <= r < 10k | ||
| a = [0] * 10000 # Array of 10k elements initialized to 0 | ||
| for i in range(10000): # 10k outer loop iterations | ||
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I already merged a PR for cython?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually, PR #222 hasn't been merged yet :) |
||
|
|
||
| if __name__ == '__main__': | ||
| # force import of cythonized module | ||
| from cythonized import main | ||
| main() | ||
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.