Chen Sturmweizen
Chen Sturmweizen

Reputation: 639

Will it improve performance if I loop through a large array in chunks?

I just came across an answer for javascript and tried out. It actually works in a way that compare to looping through the whole array, it eliminates the screen-frozen effect: https://stackoverflow.com/a/10344560/2083396

So my questions is: is there any similar way in PHP to improve performance?

My understanding is that in the javascript solution, after each timeout callback is called, its scope is destroyed. And thus the resource is released. (did I understand it correctly?)

Is there any way we could do so in PHP and does it help?

Thanks!

Upvotes: 2

Views: 1095

Answers (2)

Alex Blex
Alex Blex

Reputation: 37048

No, you didn't get it. The only variable that is destroyed there is cnt. It does not improve performance. In fact it make it slower.

The problem they are solving is that long-running js functions freeze the page because js is single-threaded. They split the long job by smaller chunks and run it with small pauses to allow js to process other events.

There is no such problem in PHP, unless you are working with long-running react-based application or something similar.

Upvotes: 2

Hitchen1
Hitchen1

Reputation: 11

My understanding is that Apache will only deliver the page when the full PHP script has finished executing, so reducing the overall execution time, rather than splitting it into chunks, will be the best way to improve performance.

Upvotes: 0

Related Questions