# PHP Performance - 1’000’000 iterations

- Thread: https://externals.io/message/62257
- Original archive: https://news-web.php.net/php.internals/62257
- Started by Raymond Irving on 2012-08-19T14:29:01+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62257 — Raymond Irving — 2012-08-19T14:29:01+00:00

URL: https://externals.io/message/62257#62257

Hello,

What could have cause PHP to start out so great but then slows to a crawl?
Could it be the GC?

Number of iterations Node.js PHP
---------------------------------------------------------------
100                         2.00         0.14
10’000                 3.00         10.53
1’000’000                 15.00 1119.24
10’000’000                 143.00 10621.46
1’000’000’000         11118.00 1036272.19

See the script here:
http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+nettuts+%28Nettuts%2B%29

Is there any way that this can be improved?

__
Raymond

---

## #62258 — Anthony Ferrara — 2012-08-19T14:46:30+00:00

URL: https://externals.io/message/62257#62258 · In reply to: #62257

> Hello,
>
> What could have cause PHP to start out so great but then slows to a crawl?
> Could it be the GC?
>
> Number of iterations Node.js PHP
> ---------------------------------------------------------------
> 100                         2.00         0.14
> 10’000                 3.00         10.53
> 1’000’000                 15.00 1119.24
> 10’000’000                 143.00 10621.46
> 1’000’000’000         11118.00 1036272.19
>
> See the script here:
>
> http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+nettuts+%28Nettuts%2B%29
>
> Is there any way that this can be improved?
>

PHP doesn't slow to a crawl. It runs in constant time. You can see that
because increasing the iteration count by 2 orders of magnitude increases
runtime by two orders of magnitude.

What you're seeing with nodejs is v8's JIT compiler in action. The
compilation step takes some extra time, which you only see the benefit from
in high iteration counts. It compiles the code down to machine code (which
prob takes the majority of the time), and then executes it. Where PHP
executes the same code each time. Which is why PHP is significantly faster
in the shorter iteration counts (it doesn't have the overhead of converting
to machine code).

Now, it looks like a surprising result. But it's far from a realistic
benchmark. For the narrow use-case where you're doing high iteration counts
over a simple (to a compiler) instruction set, v8 may be 93% faster. But
for the general use case, you won't find that big of a difference...

Nothing is free. Everything costs something. And that's the problem with
simple benchmarks like this. They hide the cost, and only show you the
result. Which is unrelistic at best...

Anthony

---

## #62261 — Raymond Irving — 2012-08-19T15:41:39+00:00

URL: https://externals.io/message/62257#62261

Many thanks for the feedback.

__
Raymond



> 
> > […]
> crawl?
> > […]
> http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+nettuts+%28Nettuts%2B%29
> > […]
>
> If you are doing something that needs to iterate a billion times, you
> shouldn't be using PHP. Things like calculating a fractal is just not
> what PHP was meant for. The vast majority of Web apps have absolutely no
> need to iterate that many times. You wouldn't try to show a user a
> billion database records on a single page, for example. Chances are
> pretty good that you are going to iterate less than 1000 times on a
> typical request and if you do end up having to iterate more for some
> reason, you would probably cache the result somewhere so you don't have
> to do it on a subsequent request.
>
> -Rasmus

---

## #62274 — Kris Craig — 2012-08-20T01:06:08+00:00

URL: https://externals.io/message/62257#62274 · In reply to: #62261

It's also worth noting that a billion iterations is a lot in any language,
not just PHP.  Plus most browsers would probably blow-up in your face if
you tried to send that much data.

--Kris




> Many thanks for the feedback.
>
> __
> Raymond
>
> On Sun, Aug 19, 2012 at 10:02 AM, Rasmus Lerdorf <rasmus@lerdorf.com>
> wrote:
>
> > […]
> http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+nettuts+%28Nettuts%2B%29
> > […]
