对象与数组 [英] Objects versus Arrays

查看:53
本文介绍了对象与数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我正在一个站点上工作,并且专注于效率,加载,处理等方面的速度.

I am working on a site at the moment, and there is a concentrated focus on efficiency and speed in loading, processing and such like.

我正在使用mysqli扩展名来获取数据库位和bob,但我想知道什么是输出数据集的最佳/最有效方法?

I'm using the mysqli extension to get my database bits and bobs, but I'm wondering what's the best / most efficient way of outputting my dataset?

此刻,我正在使用$ mysqli-> fetch_assoc()和foreach().阅读 http://www.phpbench.com 后,我知道首先计算数据会有所不同. (我将在构建后进行优化)

At the moment I'm using $mysqli->fetch_assoc() and a foreach(). Having read http://www.phpbench.com I know that counting my data first makes a difference. (I'm going to optimise after build)

我的问题是,将结果集放入php数据事物更快.创建一个对象?一个数值数组?关联数组?我的想法是一个对象,但我不确定.

My question is, which is quicker for getting a resultset into a php data thing. Creating an object? A numerical array? An associative array? My thoughts are an object, but I'm unsure.

好奇,因为我不熟悉PHP内部:)

Just curious, as I'm not familiar with the PHP internals :)

推荐答案

mysql_fetch_object的注释下,PHP文档中实际上有一个小型基准:

There is actually a small benchmark in PHP documentation under mysql_fetch_object's comments:

SELECT * FROM bench... (mysql_fetch_object)

查询时间:5.40725040436
提取时间:16.2730708122(平均:1.32130565643E-5)
总时间:21.6803212166

Query time: 5.40725040436
Fetching time: 16.2730708122 (avg: 1.32130565643E-5)
Total time: 21.6803212166

SELECT * FROM bench... (mysql_fetch_array)

查询时间:5.37693023682
提取时间:10.3851644993(平均:7.48886537552E-6)
总时间:15.7620947361

Query time: 5.37693023682
Fetching time: 10.3851644993 (avg: 7.48886537552E-6)
Total time: 15.7620947361

SELECT * FROM bench... (mysql_fetch_assoc)

查询时间:5.345921278
撷取时间:10.6170959473(平均:7.64049530029E-6)
总时间:15.9630172253

Query time: 5.345921278
Fetching time: 10.6170959473 (avg: 7.64049530029E-6)
Total time: 15.9630172253

获取对象最慢,获取数字数组可能比使用mysql_fetch_arraymysql_fetch_assoc快一点,但是差别可以忽略不计.实际上,mysql_fetch_array可以同时提取assoc和数字,并且比mysql_fetch_assoc快,那就算了..但是,如果您追求性能,那就不要使用mysql_fetch_object.

Fetching an object is slowest, fetching a numeric array is probably a bit faster than using mysql_fetch_array or mysql_fetch_assoc, but the difference is negligible. In fact, mysql_fetch_array fetches both assoc and numeric, and it's faster than mysql_fetch_assoc, go figure.. But if you're after performance, just don't use mysql_fetch_object.

这篇关于对象与数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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