0

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!

3
  • I assume you mean "DB" for Database. Commented Jan 17, 2011 at 18:41
  • yes, and i also assume the process of recovering info as using some PHP method like "fetch" which gives you an array Commented Jan 17, 2011 at 18:42
  • i would use paging if possible to limit the data shown on a page Commented Jan 17, 2011 at 18:50

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

the idea of the question was more about how a large amount of objects can influence in memory costs of the app...
you should have probably mentioned that. " i need to recover all that info and show it on a webpage" - it was misleading to make me believe you were trying to display the data in a single page. on a related note, manipulating 3000 objects is very fast in PHP. and there is no noticeable difference between 2-dimensional array of values and a list of objects. But again it will become noticeable if you are looking for micro seconds of differences or dealing with extremely high traffic websites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.