I am testing an algorithm for packing 2d bin, and I have chosen PHP in order to mock it, as this is my language of bread and butter at present.
As you can see at http://themworks.com/pack_v0.2/oopack.php?ol=1 , it works very well, but you need to wait about 10-20 seconds for 100 rectangles of the pack. For some hard-to-handle sets, it would reach the php 30 runtime limit.
I did some profiling, and it shows that most of the time my script goes through different parts of a small 2d array with 0 and 1 in it. It either checks if the specific cell is 0/1, or sets the value to 0/1. It can perform such operations a million times and each time it takes several microseconds.
I think I could use an array of logical elements in a statically typed language, and everything will be faster. Or even make an array of 1 bit values. I am going to convert all this into a compiled language. PHP just not suitable for this?
If I need to convert it to say C ++, how good are automatic converters? My script is just a lot for loops with basic arrays and object manipulations.
Change This function is called more than any other. It reads several properties of a very simple object and goes through a very small part of a small array to check if there is any element other than 0.
function fits($bin, $w, $h, $x, $y) { $w += $x; $h += $y; for ($i = $x; $i < $w; $i++) { for ($j = $y; $j < $h; $j++) { if ($bin[$i][$j] !== 0) { return false; } } } return true; }
Update: I tried using a 1d array instead of 2d as one of the suggested answers. Since I always need to have an available bin width, I decided to wrap everything in an object. In addition, now in each cycle, the index must be calculated. Now the script needs even more time to run. Other methods did not bring a big increase in productivity, but made the code less readable. I think it's time for hip hop.
Update: since hiphop php only works on linux, and I donβt have it, I decided to rewrite all this in C ++. Nice to brush up on old skills. Also, if I find a way to use hip-hop, it will be interesting to compare the C ++ code written by hand and create one hip-hop.
Update: I rewrote this thing in C ++, on average it works 20 times faster and uses much less memory. Let me see if I can do this even faster.