您的位置 首页 php

PHP代码优化之array_push

PHP中数组插入数据通常有这么几种:

  1. 定义的时候直接赋值
$arr = array(‘apple’, ’banana’);
  1. 使用数组变量操作
$arr = array();$arr[] = ‘apple’;$arr[] = ‘banana’;
  1. 使用array_push()插入
$arr = array(‘apple’);array_push($arr, ‘banana’, ‘orange’);

在项目中,我们大部分情况都是先从数据库中获取数据,然后转成数组形式,而对于第一种情况,在定义数组的时候直接赋值往往只适合数据量很小,而且是已知的情况,所以就不存在什么代码优化了,我们主要讲第二种和第三种。

性能比较

我们使用php 7.2来测试,并且没有做任何配置上的优化。测试方法:定义一个数组,分别用第二种和第三种方式插入10万条记录,代码大致如下:

PHP代码优化之array_push

PHP代码优化之array_push

convert函数用来查看内存占用情况,运行结果如下:

PHP代码优化之array_push

PHP代码优化之array_push

通过多次运行上述代码,可以发现直接使用数组变量赋值耗时在0.0045左右,而使用array_push的方式耗时在0.006左右,消耗的内存都是一样的,因为都是10万条记录。

对比两种方法,发现方法二确实比调用array_push快,因为在每次循环时函数调用需要消耗一些时间,但仅仅看上面的测试,并不觉得第二种方法比第三种方法快多少,完全可以忽略不计的。

的确,这种优化在绝大多数应用中和没优化没啥区别,但是当应用达到瓶颈的时候,每一处不起眼的优化可能都会使性能提高不少,况且我们的测试中数组的结构很简单,真实项目中数据类型不是这么单一,可能还是嵌套的数组,数组的大小可能也不止10万。

我们再来看看两者的差异,array_push()是函数调用,另一个不是,这就是说了,从代码量上看两者也是一样的,从数组结构上看,测试中我们都没有使用索引,默认都是从0开始,如果要使用key => value形式插入数据时,array_push()就不是很好处理了,这时使用法二就很方便:$array1[$key] => $x

前面都是说array_push()怎么不好,但有时候array_push还是很好用的。比如我们要一次插入多个元素时,就可以这样使用:

$arr = array(‘a’, ’b’);array_push($arr, ‘c’, ‘d’);// $arr = array(‘a’, ’b’, ‘c’, ‘d’);

通过上面对比可见,PHP中数组插值的方法有很多,虽然每种方法差不多,但了解每一种方法何时使用还是很有必要的。

相关课程:PHP视频教程

以上就是PHP代码优化之array_push的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:PHP代码优化之array_push

文章地址:https://www.zhihuclub.com/5801.shtml

关于作者: 智云科技

热门文章

评论已关闭

36条评论

  1. IMPORTANT HOW TO USE THIS INFORMATION This is a summary and does NOT have all possible information about this product

  2. Tadalafil tablets are contraindicated in patients using any form of organic nitrate; and in patients with a known serious hypersensitivity to tadalafil

  3. While Cialis and its generic forms are most commonly associated with the treatment of erectile dysfunction in men, there are other useful applications of the drug

  4. Peripheral blood and peritoneal fluid cultures were performed, and the patient was empirically treated with initial loading doses of intraperitoneal cefazolin 1000 mg and tobramycin 60 mg followed by maintenance doses of cefazolin 250 mg per each PD peritoneal dialysis and tobramycin 30 mg once a day.

  5. The prospector, who looked suspiciously like the wealthy retiree from Gilligan s Island, locked up the BradysГў and AliceГў overnight in the abandoned jail

  6. neurontin kremers omeprazole So I m thinking in their older age, they weren t as healthy as they wanted to be, she said

  7. levitra cina levofloxacino 750 precio With a cap set at Г‚ 72, 000, it is clear that only a relatively small percentage of older people will receive financial support as a result namely those who have the greatest care needs for a considerable amount of time

  8. B Modularity of each gene s bottom up coessentiality network after application of Louvain s algorithm for community detection

  9. MDA MB 468 ERО± weak ERОІ cells were injected in the mammary fat pad in nude mice as described under figure 1

  10. 1 BSA in PBS, we counted the cells and incubated equal numbers of cells for 45 min at 4 C with primary antibody APC Cy7 anti mouse CD45 Antibody BioLegend, 103115, 1 100

  11. This study is limited to dosages of 150iu and 500iu, although I hope to obtain more data in the near future

  12. With loss of kidney function, there is an accumulation of water, waste, and toxic substances in the body that are normally excreted by the kidney

  13. The propensity for risk reducing interventions weight loss, smoking cessation or visiting a physician, was 32

  14. Läkemedel Välj i första hand receptfria NSAID, Ibumetin ibuprofen 400 mg 1 tablett vid debut och eller Panodil paracetamol tablett I know you are worried about his safety, but think about if he can beat even me, will keeping him in God is Domain all the time is sweet n low ok for diabetics limit his development said God Venerable

  15. epidermidis coagulase positive and coagulase negative, including penicillin resistant strains Cyclists accounted for one in 14 of people killed on roads north of the Border

  16. Oral administration of milrinone to pregnant rats and rabbits during organogenesis produced no evidence of teratogenicity at dose levels up to 40 mg kg day and 12 mg kg day, respectively

  17. safety regulators at the National Highway Traffic Safety Administration had opened a preliminary evaluation into an estimated 40, 000 Genesis cars from model year 2009 after receiving 23 consumer complaints alleging reduced brake effectiveness, according to documents filed online

  18. Side Effects Nausea, Vomiting, Diarrhea, Altered Taste, Alopecia, Ulcerative Stomatitis bone marrow suppression leukopenia, anemia, thrombocytopenia; increase uric acid level Tamoxifen irreversible visual change temporary vaginal bleeding Most importantly, the following drugs are to be avoided when Janumet is used, due to a heightened risk of lactic acidosis

  19. 82 95 confidence interval, 0 Nucleocytoplasmic localization of proteins can be disrupted by different mechanisms in cancer cells

网站地图