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.
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?
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)
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.
Just curious, as I'm not familiar with the PHP internals :)
There is actually a small benchmark in PHP documentation under
mysql_fetch_object
's comments:Fetching an object is slowest, fetching a numeric array is probably a bit faster than using
mysql_fetch_array
ormysql_fetch_assoc
, but the difference is negligible. In fact,mysql_fetch_array
fetches both assoc and numeric, and it's faster thanmysql_fetch_assoc
, go figure.. But if you're after performance, just don't usemysql_fetch_object
.I believe a numerical array is likely to be the most lightweight, followed by associative array, and then an object. I don't think the differences will add up to much, so whichever syntax you're most comfortable with is best.
From the manual page of
mysql_fetch_object()
As the benchmark given by Tatu suggests, there is a slight difference, but keep in mind the numbers have been cumulated from 100 consecutive queries. I'd say your strategy of not bothering now and optimize later is a good choice.