KieranP / Game-Of-Life-Implementations

Conway's Game of Life implementation in various languages
MIT License
52 stars 8 forks source link

PHP is throwing undefined index errors. #1

Closed troynt closed 13 years ago

troynt commented 13 years ago

PHP is throwing undefined index errors.

I fixed this for you.

KieranP commented 13 years ago

Thanks for this. Have merged them.

Why switch out array_filter for a custom foreach and append? Was it much faster?

Edit: Did a quick check. Looks like manually appending is faster. Over 10000 uses of the functions, I get:

append_to_array: 0.035 array_filter: 0.066

Nearly twice as fast. So nice find. Thanks for that.

KieranP commented 13 years ago

Oh, by the way, the changes, which look good, didn't actually bring a performance boost for me... tests run fine, but tick times are still averaging 0.129s on my computer. How do you run the script? I use "php play.rb", using PHP 5.3.6.

KieranP commented 13 years ago

@troynt I've copied the directions array changes to the Javascript and Ruby implementations (they all need to have the closest implementation to make the benchmarks meaningful).

Using @ to catch errors raised the rendering time from 0.018s on average to 0.026s on average. I removed the use of @ in cell_at function, and instead wrapped it with an isset conditional. It brings the average down the 0.026s to a lower 0.023s. Still not ideal, but I do think the app should run without any warnings and errors, and if the speed slowdown means that, so be it.

Combined with some other changes of mine, the tick time is down from 0.129s to 0.107s.

The total tick and render time goes from 0.147s down to 0.130s, a 17ms speed boost.

Still no where near the Ruby or Javascript implementations speeds.

Any ideas for how to speed it up further?

troynt commented 13 years ago

I'll check it out again tonight. I profiled it last night, most of the time is spent calculating neighbors -- neighbours_at & alive_neighbours_at.

KieranP commented 13 years ago

I did a bunch of investigation. Found the foreach loops to be the cause. Cannot figure out why they are the bottleneck though.

In the process of debugging the PHP version, I found ways to optimize all implementations (see d30bb4531f4acafc7c413d1c669bbf0490412a12).

If you're keen, lets continue discussion bout the PHP speed issues at https://github.com/KieranP/Game-Of-Life-Implementations/issues/2