Y
Y
Ytsu Ytsuevich2015-05-19 20:19:48
PHP
Ytsu Ytsuevich, 2015-05-19 20:19:48

PHP. Are strings or arrays faster?

I have been thinking for a very long time which is better to use. PHP doesn't have StringBuffer or StringBuilder like Java/C#
does . So which is faster:

$array = array();
$array[] = 'First ';
$array[] = 'Second ';
$array[] = 'Three';
// в конце implode

// или

$str = '';
$str .= 'First ';
$str .= 'Second ';
$str .= 'Three';

Concatenation seems to be a slow operation in all languages ​​(and in PHP).
After all, strings are immutable objects.
But creating a new cell in an array is also not a cheap operation, since an array in PHP is associative!
So which is better???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2015-05-19
@kofon

better score: the difference will be noticeable with a nanostopwatch.

O
OnYourLips, 2015-05-19
@OnYourLips

It will be faster with a template engine. Because it will be faster to develop, and no one cares how long it usually takes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question