Raul Leaño Martinet
Raul Leaño Martinet

Reputation: 2113

php optimization

let's say i have a web app with 3000 books, all the info is in a DB, and let's say i need to recover all that info and show it on a webpage. which one of these two approches is better in terms of memory consumption?

  1. Recover the info from the DB, convert that info into 3000 Book Objects and parse all of them showing the html result.
  2. Just recover the info from the DB and parse directly the result array into html.

probably the question is "does creating lots of objects in PHP creates memory issues?"

thanks!

Upvotes: 0

Views: 196

Answers (2)

Murukesh
Murukesh

Reputation: 610

Seriously, why don't you use traditional paging or infinite scrolling (like twitter) ? Using the mysql LIMIT keyword, you can fetch only a small subset of records and show it per page. That will be very fast, scalable and suggested for displaying large amount of data. There are tons of tutorials like this online.

manipulating 3000 objects is normally very fast in PHP, but again the performance would depend on the website traffic/server configuration, the amount of data you fetch for a single book and the latency between DB and web server. But even if the PHP code is faster and you end up outputting 3000 records to the browser, the HTML may slowdown the browser and I cannot conceive any meaningful benefit it will provide to the user by showing them all 3000 records.

Upvotes: 1

zzzzBov
zzzzBov

Reputation: 179256

Does creating lots of objects in PHP create memory issues?

yes it does, but they're very manageable.

The fastest/most efficient way is to parse the results directly into HTML output in one giant loop, but it's not very manageable when it comes to coding/extending

PHP with the OOP paradigm is a bit of a memory hog, but it provides some nice structure:

If you parse each entity into an object, and have the object print to the page, you can kill off each object instance. Alternatively you can gather a cluster of book instances, and parse them in chunks.

Upvotes: 5

Related Questions