for vs foreach vs while 在php中迭代数组更快 [英] for vs foreach vs while which is faster for iterating through arrays in php

查看:34
本文介绍了for vs foreach vs while 在php中迭代数组更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个是在 php 中迭代数组最快的?或者是否存在另一个更快的数组迭代?

which one is the fastest for iterating through arrays in php? or does another exist which is also faster for iterating through arrays?

推荐答案

即使有任何差异,差异也会很小,根本不重要.

Even if there is any kind of difference, that difference will be so small it won't matter at all.

如果你有一个对数据库的查询,与循环迭代结果相比,forforeach 的永恒争论相比,它会花费很长时间> vs while 不会改变任何事情——至少如果你有合理数量的数据.

If you have, say, one query to the database, it'll take so long compared to the loop iterating over the results that the eternal debate of for vs foreach vs while will not change a thing -- at least if you have a reasonable amount of data.

所以,使用:

  • 随便你
  • 适合您的编程标准的任何内容
  • 最适合您的代码/应用程序的任何内容

在考虑这种微优化之前,您可以/应该优化很多其他事情.

There will be plenty of other things you could/should optimize before thinking about that kind of micro-optimization.


如果你真的想要一些数字(即使只是为了好玩),你可以做一些基准测试并在实践中查看结果.


And if you really want some numbers (even if it's just for fun), you can make some benchmark and see the results in practice.

这篇关于for vs foreach vs while 在php中迭代数组更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆