如何在PHP中正确循环数组键

时间:2017-11-16 16:48:15

标签: php arrays performance loops coding-style

如果我有一个包含多个键和值的数组,就像这样。

$array = array(
  'key1' => 'value1',
  'key2' => 'value2',
);

当我只使用密钥时,是否有任何标准循环数组? 解决方案1:

foreach(array_keys($array) as $array_key) {
  echo $array_key;
}

解决方案2:

foreach($array as $array_key => $array_value) {
  echo $array_key;
}

我可以看到解决方案1的优势,因为它不会调用未使用的变量$array_value这两者之间的表现是否存在差异?有没有标准怎么做?

1 个答案:

答案 0 :(得分:3)

从我快速而肮脏的基准测试中,我做了以下50次:

解决方案1 ​​(最差):

foreach (array_keys($array) as $array_key) {
  echo $array_key;
}

数组大小:1000000

  • 分钟 0.11363291740417
  • 平均: 0.11681462287903
  • 最高: 0.14569497108459

阵列大小:9999999

  • 分钟 1.3098199367523
  • 平均: 1.3250577354431
  • 最高: 1.3673560619354

解决方案2 (中):

foreach ($array as $array_key => $array_value) {
  echo $array_key;
}

数组大小:1000000

  • 分钟 0.10167503356934
  • 平均: 0.10356130123138
  • 最高: 0.11027193069458

阵列大小:9999999

  • 分钟 1.2077870368958
  • 平均: 1.2256314325333
  • 最高: 1.2829539775848

解决方案3 (最佳):

$array_keys = array_keys($array);
foreach ($array_keys as $array_key) {
  echo $array_key;
}

数组大小:1000000

  • 分钟 0.090911865234375
  • 平均: 0.092938437461853
  • 最高: 0.097810983657837

阵列大小:9999999

  • 分钟 1.0793349742889
  • 平均: 1.0941110134125
  • 最高: 1.1402878761292

因此,如果您只想查看数组键,可以看到解决方案3是最快的选项:)

希望这有帮助。

代码:

<?php
class DirtyBenchmarker {
  private $results = [];
  private $size_of_array;

  public function __construct($size_of_array)
  {
    $this->size_of_array = $size_of_array;
    echo 'Size of array: ' .  $this->size_of_array . PHP_EOL;
  }

  private function solution1() {
    $array = range(0, $this->size_of_array - 1);
    ob_start();
    $start = microtime(true);
    foreach (array_keys($array) as $array_key) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution1'][] = $finish;
  }

  private function solution2() {
    $array = range(0, $this->size_of_array - 1);
    ob_start();
    $start = microtime(true);
    foreach ($array as $array_key => $array_value) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution2'][] = $finish;
  }

  private function solution3() {
    $array = range(0, $this->size_of_array - 1);
    $array_keys = array_keys($array);
    ob_start();
    $start = microtime(true);
    foreach ($array_keys as $array_key) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution3'][] = $finish;
  }

  public function benchmark() {
    $this->solution1();
    $this->solution2();
    $this->solution3();
  }

  public function getResults()
  {
    echo PHP_EOL . 'Solution 1:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution1']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution1']) / count($this->results['solution1']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution1']) . PHP_EOL;

    echo PHP_EOL . 'Solution 2:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution2']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution2']) / count($this->results['solution2']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution2']) . PHP_EOL;

    echo PHP_EOL . 'Solution 3:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution3']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution3']) / count($this->results['solution3']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution3']) . PHP_EOL;
  }
}

$benchmarker = new DirtyBenchmarker(1000000);
$runs = 50;
for ($i = 0; $i < $runs; $i++) {
  $benchmarker->benchmark();
}
$benchmarker->getResults();

$benchmarker = new DirtyBenchmarker(9999999);
$runs = 50;
for ($i = 0; $i < $runs; $i++) {
  $benchmarker->benchmark();
}
$benchmarker->getResults();