php里push的用法,PHP array_push 用法 手册 | 示例代码

I did a performance check, and I saw, if you push more than one value it can be faster the array push, that the normal $array[] version.

Case 1: $array[] = something;

Case 2: array_push($array, $value);

Case 3: array_push($array, $value1, $value2, $value3 [...]); $values are definied

Case 4: array_push($array, $value1, $value2, $value3 [...]); $values are definied, when $array is not empty

Case 5: Case1 + Case 3

Case 6: Result array contains some value (Case 4)

Case 7: Result array contains same value as the push array (Case 4)

-----------------------------------------------------------------------------------------------------------

~~~~~~~~~~~~ Case 1 ~~~~~~~~~~~~

Times: 0.0310 0.0300 0.0290 0.0340 0.0400 0.0440 0.0480 0.0550 0.0570 0.0570

Min: 0.0290

Max: 0.0570

Avg: 0.0425

~~~~~~~~~~~~ Case 2 ~~~~~~~~~~~~

Times: 0.3890 0.3850 0.3770 0.4110 0.4020 0.3980 0.4020 0.4060 0.4130 0.4200

Min: 0.3770

Max: 0.4200

Avg: 0.4003

~~~~~~~~~~~~ Case 3 ~~~~~~~~~~~~

Times: 0.0200 0.0220 0.0240 0.0340 0.0360 0.0410 0.0460 0.0500 0.0520 0.0520

Min: 0.0200

Max: 0.0520

Avg: 0.0377

~~~~~~~~~~~~ Case 4 ~~~~~~~~~~~~

Times: 0.0200 0.0250 0.0230 0.0260 0.0330 0.0390 0.0460 0.0510 0.0520 0.0520

Min: 0.0200

Max: 0.0520

Avg: 0.0367

~~~~~~~~~~~~ Case 5 ~~~~~~~~~~~~

Times: 0.0260 0.0250 0.0370 0.0360 0.0390 0.0440 0.0510 0.0520 0.0530 0.0560

Min: 0.0250

Max: 0.0560

Avg: 0.0419

~~~~~~~~~~~~ Case 6 ~~~~~~~~~~~~

Times: 0.0340 0.0280 0.0370 0.0410 0.0450 0.0480 0.0560 0.0580 0.0580 0.0570

Min: 0.0280

Max: 0.0580

Avg: 0.0462

~~~~~~~~~~~~ Case 7 ~~~~~~~~~~~~

Times: 0.0290 0.0270 0.0350 0.0410 0.0430 0.0470 0.0540 0.0540 0.0550 0.0550

Min: 0.0270

Max: 0.0550

Avg: 0.044

Tester code:

// Case 1

$startTime = microtime(true);

$array = array();

for ($x = 1; $x <= 100000; $x++)

{

$array[] = $x;

}

$endTime = microtime(true);

// Case 2

$startTime = microtime(true);

$array = array();

for ($x = 1; $x <= 100000; $x++)

{

array_push($array, $x);

}

$endTime = microtime(true);

// Case 3

$result = array();

$array2 = array(&$result)+$array;

$startTime = microtime(true);

call_user_func_array("array_push", $array2);

$endTime = microtime(true);

// Case 4

$result = array();

for ($x = 1; $x <= 100000; $x++)

{

$result[] = $x;

}

$array2 = array(&$result)+$array;

$startTime = microtime(true);

call_user_func_array("array_push", $array2);

$endTime = microtime(true);

// Case 5

$result = array();

$startTime = microtime(true);

$array = array(&$result);

for ($x = 1; $x <= 100000; $x++)

{

$array[] = $x;

}

$endTime = microtime(true);

// Case 6

$result = array(1,2,3,4,5,6);

$startTime = microtime(true);

$array = array(&$result);

for ($x = 1; $x <= 100000; $x++)

{

$array[] = $x;

}

$endTime = microtime(true);

// Case 7

$result = array();

for ($x = 1; $x <= 100000; $x++)

{

$result[] = $x;

}

$startTime = microtime(true);

$array = array(&$result);

for ($x = 1; $x <= 100000; $x++)

{

$array[] = $x;

}

$endTime = microtime(true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值