Message331280
For some ranges of inputs, it may make sense to use the apparently naive implementation `factorial(n) // factorial(k) // factorial(n - k)`. The current factorial implementation is significantly optimised, and using it directly may be faster than using an iterative solution.
Here are some timings (Python 3.7.1, macOS 10.13), using Raymond's `comb` function from msg331257:
In [5]: %timeit comb(1000, 600)
186 µs ± 442 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: %timeit factorial(1000) // factorial(600) // factorial(400)
97.8 µs ± 256 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [7]: %timeit factorial(1000) // (factorial(600) * factorial(400))
91.1 µs ± 789 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
But that's just one set of inputs, on one system; your results may vary. |
|
| Date |
User |
Action |
Args |
| 2018-12-07 08:42:05 | mark.dickinson | set | recipients:
+ mark.dickinson, tim.peters, rhettinger, steven.daprano, kellerfuchs |
| 2018-12-07 08:42:05 | mark.dickinson | set | messageid: <[email protected]> |
| 2018-12-07 08:42:05 | mark.dickinson | link | issue35431 messages |
| 2018-12-07 08:42:05 | mark.dickinson | create | |
|