Page 3 of 3

Posted: Sun Sep 10, 2006 1:40 am
by n00b Saibot
Mordred wrote:Your 230230 combinations of 20 elements mean something like 4 604 600 bytes + the array overhead = ~5M memory
If you go and just put the result in a foreach() like you did in the first example - whoops, there go another 5M
8O That explains memory problems I'm having currently with my data miner :evil: over 697 main categories averaging about 20 subcategories each having about 50 elements.. think how big would be the array and the required memory... and yes I do run foreach on it too :lol: :oops:
Mordred wrote:Iterating through all of them with a callback function will mean that only one array of 20 elements will be kept in memory (or maybe two if you pass it to the callback without a reference) -- i.e. a negligible amount.
nice insight Mordred, thanks
Mordred wrote:Using a high-level language is no excuse for being careless with memory and CPU!
yep.. you're right! and I was being careless.

Posted: Sun Sep 10, 2006 3:33 am
by bokehman
sweatje wrote:How about using n00b Saibot's approach but making it dynamic.
Brilliant! Obviously needs a numerically keyed array with suquencial keys starting at zero so a good failsafe would be to add $input = array_values($input); on the first line of the function.

I'm creating with a poker class at the minute and wanted a method to work out "the nuts" (i.e. the hand with the highest possible rank based on the community cards). I made a method based on brute force (nothing to do with this function) and it took 4.2 seconds to run. So I made one that worked out the best hand based on logic rather than brute force. The code is not perfect but the speed increase is amazing. Depending on the hand it takes between 200-900 microseconds to run. Anyway due to these findings I am trying to steer clear of anything that relies on brute force which is why I like this function so much. Thanks for your input on this.
Mordred wrote:I really do recommend using a callback function against generating all combinations in a 2d array.
The truth is the arrays aren't that big but I'm interested in your concept. The problem is this is a method in a class and it will be used by several other methods. In such an application how would you implement your callback code?