Closed gurvesh closed 3 years ago
Thanks for the comment. These comments are very welcomed and can be read by those that dive into the details. The book's source code does not attempt to always be the most efficient code. We agree that your suggestion is an improvement and may incorporate in future versions.
Hi. Many thanks for the book! I'm really grateful for this.
On this particular example, the following code is quite inefficient (uses >1GB memory and takes >1.5 secs for n=5) - even on the 2nd pass after compilation. It also fails (insufficient memory etc) pretty quickly as n increases.
Something better would be to use the combinations function instead to get (2n,n) places, and populate them with 1s etc. The following runs easily 2 orders of magnitude faster (on the 2nd pass once compiled):
This takes < 0.04s for n=5, and <5MB memory. This code finishes in <0.2s for n=10 as well.