在写PHP 程序的时候,很多人在传递参数的时候,喜欢用一个引用。特别是在一个数组非常的大的时候,更是喜欢加。
function binsearch(&$arr, $key, $value)
{
$low = 0;
$high = count($arr);
while ($low <= $high) {
$mid = floor($low + ($high – $low) / 2);
$item = $arr[$mid][$key];
if ($item == $value) {
return $mid;
} else if ($value > $item) {
$low = $mid + 1;
} else {
$high = $mid – 1;
}
}
return false;
}
在这里,$mid 采用了先减后加的方法计算,目的是为了防止整数的溢出。不是故意写复杂了。
我用下面的代码进行测试:
$data = array();
for ($i = 0; $i < 1000000; $i++)
{
$data[] = array("sq" => $i * 2);
}
var_dump(binsearch($data, "sq", 10000));
发现,binsearch 的时候,总是要花个 0.2s左右。理论上来说,100万的数据,最多也就是循环20次。怎么会这样慢呢。
后来监控了一下内存,data 数组 占用了 230M 的内存。而 binsearch 的时候,占用了60K 的内存。但是,理论上来说,binsearch
不应该占用如此多的内存。因为,我觉得,我已经用引用了,根本就没有对data 的结构进行修改。
我也是百思不得其解,后来,我把引用参数去掉,居然 binsearch 只要 0.0002s ,看来是引用耗费了大量的cpu 资源。
PHP 内部遵循一个copy on write 的原则。实际上这个引用是多余的。
但是为什么,加了引用速度会变慢呢?今天重点就谈谈这个问题。明白道理后,大家一定知道怎么用引用了。
如果在binsearch 调用前,直接 $a = &$data,这个引用的速度会非常的快。看来肯定不是引用本身产生的问题。
这个问题,实际上涉及了zend 引擎如何管理PHP变量。
波比源码 » PHP开发经验:引用是个坏习惯
buy levaquin without prescription levofloxacin 500mg us
tadalafil 5mg for sale tadalafil cialis sildenafil 100mg drug
doxycycline uk doxycycline 100mg cheap furosemide 40mg tablet
altace 5mg pill altace usa buy azelastine 10ml for sale
cost alendronate buy fosamax 35mg pill famotidine cheap
order prograf generic purchase labetalol pill generic urso 150mg
buy generic isosorbide isosorbide price micardis 20mg for sale
generic bupropion order strattera 25mg for sale seroquel 100mg oral
order sertraline 100mg without prescription order kamagra pill buy viagra for sale
order medroxyprogesterone for sale microzide 25mg cheap periactin order
fluvoxamine 100mg generic glipizide 5mg over the counter glucotrol oral
accutane for sale order accutane 10mg generic deltasone 20mg uk
buy piracetam 800 mg generic canadian viagra and healthcare purchase sildenafil online
purchase norvasc online order cialis 10mg without prescription order tadalafil 20mg online cheap
order clozaril 100mg order dexamethasone without prescription decadron price
buy zyvox sale linezolid for sale online casino games free
essay helpers pre written essays for sale blackjack for money
clomid 50mg usa play live poker online blackjack online us
aristocort 10mg oral loratadine over the counter order clarinex 5mg without prescription
buy methotrexate 5mg sale order metoclopramide 10mg without prescription reglan 20mg tablet
cozaar 25mg generic topiramate 200mg cost topiramate sale
imitrex 25mg tablet avodart 0.5mg without prescription order avodart 0.5mg without prescription
order flomax 0.2mg sale spironolactone 25mg ca buy aldactone online
purchase flagyl online clavulanate over the counter bactrim over the counter
purchase diflucan buy sildenafil 50mg sale oral sildenafil 50mg
buy keflex sale order keflex generic order erythromycin 250mg sale
play blackjack online free poker games tadalafil 5mg for sale
term paper service stromectol 12mg over the counter ivermectin topical
sildenafil on line buy lamictal 50mg online cheap lamictal buy online
buy zithromax 250mg for sale neurontin 100mg ca neurontin 600mg without prescription
poker online best online slot games modafinil 100mg price
furosemide 40mg pill furosemide 40mg brand hydroxychloroquine 200mg oral
order generic fildena 50mg tamoxifen pill purchase budesonide without prescription
order tadacip 20mg online cheap voltaren 50mg tablet indomethacin 50mg sale
clarithromycin pills order meclizine 25mg online antivert 25mg cost
order naproxen pill order prevacid sale order prevacid 15mg sale
order tiotropium bromide 9 mcg online cheap cost minocin oral terazosin
montelukast 10mg over the counter buy generic montelukast 10mg viagra next day delivery
san manuel casino online play poker online for real money online gambling casinos
ivermectin 1% cream generic order dapsone 100mg for sale order avlosulfon 100 mg
play roulette online real money roulette online play roulette free for fun
order nifedipine 10mg for sale order perindopril 8mg sale fexofenadine 180mg sale
poker online sites online gambling real money my future essay writing
altace usa arcoxia 60mg pills buy arcoxia 120mg pill
doxycycline price order doxycycline for sale order cleocin 300mg generic
benicar 20mg generic buy depakote 250mg generic order divalproex 250mg for sale
mesalamine cost mesalamine price order avapro for sale
generic nortriptyline oral paxil 20mg paxil 10mg brand
ropinirole over the counter order labetalol 100 mg without prescription cost trandate
buy cialis 5mg pills Cialis pharmacy order sildenafil 50mg for sale
Thank you for great information. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
cialis 40mg canada Cialis generic buy ed pills generic
buy absorica sale generic deltasone buy ampicillin generic