开发者

PHP: many concats or one implode?

In my current project I assemble one string out to many small strings (direct output is NOT an option). Would it be more efficient to do many string concatenations? or should I add the parts to an array and 开发者_运维百科implode it?


First a side note - any of this does not matter in a real production application, as the time differences are superficial and the optimization of the application should be done in other places (dealing with network, database, filesystem, etc.). That being said, for curiosity's sake:

implode may be more efficient concatenation, but only if you already have the array. If you don't, it probably would be slower since all the gain would be offset by the time needed to create the array and allocate its elements. So keep it simple :)


<?php
  function microtime_float()
  {
      list($usec, $sec) = explode(" ", microtime());
      return ((float)$usec + (float)$sec);
  }

  define('ITERATIONS', 10000);
  header('Content-Type: text/plain');

  printf("Starting benchmark, over %d iterations:\r\n\r\n", ITERATIONS);

  print("Imploding...");
  $start = microtime_float();

  $list = Array();
  for ($_ = 0; $_ < ITERATIONS; $_++)
    $list[] = 'a';
  $result = implode('',$list);

  $end = microtime_float() - $start;
  printf("%0.3f seconds\r\n", $end);

  unset($list,$result);

  print("Concatenating...");
  $start = microtime_float();

  $result = '';
  for ($_ = 0; $_ < ITERATIONS; $_++)
    $result .= 'a';

  $end = microtime_float() - $start;
  printf("%0.3f seconds\r\n", $end);
?>

results in the implode taking longer 99% of the time. e.g.

Starting benchmark, over 10000 iterations:

Imploding...0.007 seconds
Concatenating...0.003 seconds


The optimization would be little compared to network traffic, databases, files, graphics, etc. However, here is a reference on the topic from sitepoint.

http://www.sitepoint.com/high-performance-string-concatenation-in-php/

Which is fastest?

The good news is that PHP5 is quick. I tested version 5.3 and you’re far more likely to run out of memory than experience performance issues. However, the array implode method typically takes twice as long as the standard concatenation operator. A comparable period of time is required to concatenate the string or build the array, but the implode function doubles the effort.

Unsurprisingly, PHP is optimized for string handling and the dot operator will be the fastest concatenation method in most cases.


As of php 7.4.3

Script1.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = '';
for ($i = 0; $i < 500000; $i++) {
    $array .=$i . ',';
}
echo $array . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.11040306091309 Seconds
// 7.9923934936523 MB
Script2.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = array();
for ($i = 0; $i < 500000; $i++) {
    array_push($array, $i);
}
echo implode(",", $array) . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.32534003257751 Seconds
// 21.992469787598 MB
Script3.php
$startMemory = memory_get_usage();
$t = microtime(true);
$array = array();
for ($i = 0; $i < 500000; $i++) {
    $array[] = $i;
}
echo implode(",", $array) . '<br>';
$time = (microtime(true) - $t) . ' Seconds<br>';
$memory = (memory_get_usage() - $startMemory) / 1024 / 1024 . ' MB <br>';
echo $time . $memory;
// 0.087002038955688 Seconds
// 21.992446899414 MB

Implode function is double faster than string concatenation. But memory usage is very less for string concatenation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜