# str_repeat performance

- Thread: https://externals.io/message/118596
- Original archive: https://news-web.php.net/php.internals/118596
- Started by Eugene Sidelnyk on 2022-09-10T12:19:44+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

---

## #118596 — Eugene Sidelnyk — 2022-09-10T12:19:44+00:00

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

Hello internals!

I have noted some strange points about `str_repeat` performance. It looks
like the lesser `$times` argument is, the bigger is execution time (only if
we consider big numbers).

For example, `str_repeat('%d ', 12500);` is a lot slower than
`str_repeat('%d ', 1250000)`.

First case (slow one):
https://3v4l.org/ZAoGi

Second case (fast one):
https://3v4l.org/YIdQM

Best regards,
Yevhen

---

## #118597 — Christoph M. Becker — 2022-09-10T12:23:18+00:00

URL: https://externals.io/message/118596#118597 · In reply to: #118596

> I have noted some strange points about `str_repeat` performance. It looks
> like the lesser `$times` argument is, the bigger is execution time (only if
> we consider big numbers).
>
> For example, `str_repeat('%d ', 12500);` is a lot slower than
> `str_repeat('%d ', 1250000)`.
>
> First case (slow one):
> https://3v4l.org/ZAoGi
>
> Second case (fast one):
> https://3v4l.org/YIdQM

It seems you have misinterpreted the results.  4.792213439941406E-5 is
certainly much smaller than 0.013129949569702148, for instance.

--
Christoph M. Becker

---

## #118602 — Eugene Sidelnyk — 2022-09-10T15:10:43+00:00

URL: https://externals.io/message/118596#118602 · In reply to: #118597

Oh, yes. Sorry for that.

Thanks for pointing out.

On Sat, Sep 10, 2022 at 3:23 PM Christoph M. Becker <cmbecker69@gmx.de>
wrote:

> 
>
> > […]
> if
> > […]
>
> It seems you have misinterpreted the results.  4.792213439941406E-5 is
> certainly much smaller than 0.013129949569702148, for instance.
>
> --
> Christoph M. Becker

---

## #118603 — Hans Henrik Bergan — 2022-09-10T16:40:05+00:00

URL: https://externals.io/message/118596#118603 · In reply to: #118602

maybe you'll find
$t=microtime(true) - $startTime;
var_dump($t,number_format($t,10));

interesting, https://3v4l.org/Ejia3



> Oh, yes. Sorry for that.
>
> Thanks for pointing out.
>
> On Sat, Sep 10, 2022 at 3:23 PM Christoph M. Becker <cmbecker69@gmx.de>
> wrote:
>
> > […]
> looks
> > […]
> (only
> > […]
