Skip to content

Instantly share code, notes, and snippets.

@shyazusa
Last active February 27, 2020 06:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyazusa/7bbcb20453e662d07b8483d248eb834c to your computer and use it in GitHub Desktop.
Save shyazusa/7bbcb20453e662d07b8483d248eb834c to your computer and use it in GitHub Desktop.
PHPで配列追加速度調査

PHPで配列/連想配列追加速度調査

配列の部

結果

  1. []
  2. +=
  3. array_push()
使用したもの 実行速度 (秒)
[] 0.10734009742737
+= 0.13818192481995
array_push() 0.14035105705261

連想配列の部

結果

  1. +=
  2. array_merge()
  3. []
使用したもの 実行速度 (秒)
+= 0.15385508537292
array_merge() 0.24352407455444
[] 0.25776886940002
<?php
// []で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
$array[] = $i;
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.10734009742737 秒
// +=で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
$array += array($i => $i);
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.13818192481995 秒
// array_push()で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
array_push($array, $i);
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.14035105705261 秒
// 結果
// 1. [] 0.10734009742737 秒
// 2. += 0.13818192481995 秒
// 3. array_push() 0.14035105705261 秒
<?php
ini_set('memory_limit', '256M');
// +=で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
$array += array($i => $i);
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.15385508537292 秒
// array_merge()で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
array_merge($array, array($i => $i));
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.24352407455444 秒
// []で配列追加
$array = array(0, 1, 2, 3, 4, 5);
$time_start = microtime(true);
for ($i=6; $i < 500000; $i++) {
$array[] = array($i => $i);
}
$time = microtime(true) - $time_start;
echo "{$time} 秒\n"; // 0.25776886940002 秒
// 結果
// 1. += 0.15385508537292 秒
// 2. array_merge() 0.24352407455444 秒
// 3. [] 0.25776886940002 秒
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment