Which Is Faster $variable[] or array

This question already has an answer here:

  • What's better to use in PHP $array[] = $value or array_push($array, $value)? 10 answers

  • http://www.php.net/manual/en/function.array-push.php#53289

    Empy bracket doesn't check if a variable is an array first as array_push does. If array_push finds that a variable isn't an array it prints a Warning message if E_ALL error reporting is on.

    So array_push is safer than [], until further this is changed by the PHP developers.

    $variable[] seems to be a lot faster:

    http://www.php.net/manual/en/function.array-push.php#83388

    However if you're adding multiple values per iteration array_push() is faster:

    http://www.php.net/manual/en/function.array-push.php#84959

    But please remember that pre-optimization is the root of all evil. Use whatever you feel more comfortable with, and when you have a performance issue, use a profiler and do some benchmarking.


    Stop doing weird things and take profiler to get real bottleneck.

    ps: there is no difference.

    pps: here is typical algo:

  • Does the application speed satisfy you? If no - step 2, if yes - step 6
  • Take the profiler
  • Find the slowest part
  • Optimize it so it become faster
  • Go to step 1
  • Get some beer
  • 链接地址: http://www.djcxy.com/p/59404.html

    上一篇: 使用循环来创建数组

    下一篇: 哪个更快$ variable []或数组