Welcome Guest, Not a member yet? Register   Sign In
Looping through views performance
#1

[eluser]albertleao[/eluser]
I'm sure this has been tested somewhere but I haven't found a clear answer.

I have a stream of data separated into posts. The posts can be fairly complicated so I isolated each individual type of post into its own view. I have a helper function which I call in my main view file called "create_feed($feed)". In this helper function I loop through the posts to create the views. My question is the following:

Is it faster to create a single function that outputs HTML for the view or to call a view multiple times?

For formatting purposes I need the view/function to only return the data. So I currently wrap ob_start() and ob_get_clean() to get the output of the function to then output them to my user.

Is this slower than just looping through views? It's been so long that I coded this but I believe at some point someone said that once a function is loaded in php, you can iterate through it much faster than calling a view 100 times.

#2

[eluser]CroNiX[/eluser]
I'm not sure why you would need to load the same view multiple times. That kind of implies that you could call it once, but in the view loop through the data passed to it to render the output for each item (posts).

Code:
<?php foreach($posts as $post): ?>
<div class="post">
  &lt;?=$post->body; ?&gt;
</div>
&lt;?php endforeach; ?&gt;

You do know that you can return a view, instead of just outputting it? No need for manually calling ob_start(), etc. I believe it does that behind the scenes, but just set the 3rd parameter to TRUE to retrieve it.

Code:
$rendered_view = $this->load->view('view_file', $data, TRUE);

You always have the option to run $this->output->enable_profiler(); to see which is faster, but I suspect that doing it all in a single view would be much faster than loading a separate view, which is a file from the filesystem, in a loop.
#3

[eluser]albertleao[/eluser]
The reason I'm looping through is because I sometimes bring in completely separate types of posts to the same feed. For example I have a post which is a post from Facebook and another from Twitter.

In some portions of my site, only the facebook feeds are shown which uses facebook post html and the same goes for twitter.

In other portions of my site, I combine both facebook and twitter posts, requiring me to pull the html for a facebook post and that of a twitter post so I need to have an individual view for each type of post to then combine them. Make sense?

I did a few benchmarks, and it's taking a full 3-4 seconds to pull the html together using the functions as I described above. The data is cached so I know that I'm guessing it's my process of putting the html together that is taking forever.
#4

[eluser]CroNiX[/eluser]
Are you caching the rendered view files?
#5

[eluser]albertleao[/eluser]
No, I'm caching the call to my model which grabs the information on a per user basis. The views are different for every user. According to my benchmarks, fetching the information is done in less than 0.1 seconds, but rendering the views is taking upwards of 3 to almost 4 seconds.
#6

[eluser]jonez[/eluser]
1) For performance, this is the fastest method:

- Controller runs, creates a large JSON object of all posts (just data, no views)
- Page loads with a blank container div
- On load, JS loops JSON passed by the controller, build views on the client (doT.js is fastest)

No view reading on the server (disk access) only data. Client renders quickly since view construct runs asynchronously. Use document fragments vs appending to live nodes to prevent browser repaints between iterations. doT also supports caching view templates for blazing fast compilation.

1a) If you don't want to get into client side templating you can create placeholder div's, clone them, inject data into them, then append. If you call this method via AJAX this is ideal since you are only passing data back and forth not compiled views (much smaller transport size).

2) Second fastest:

- Controller runs, creates a large array of all posts
- Read your template file once manually with custom placeholders for data (not using a CI view)
- Loop your data, copy the template variable and use string/replace or regex to inject variables into the view for each post
- Suggestion: {{key_name}} for variables, where key_name is the key from your data array. To compile a view loop the post's data and search/replace key for value.

As far as I know CI's view method does not cache pre-compiled views. Every time you call view it's going to access the disk and read the file. This method is basically creating your own templating system so you can only read once and compile multiple times.

It's been a while since I've benchmarked string concatenation in PHP, years ago (and in some languages still today) it was faster to do this:

Code:
$result = array( );

foreach ( $posts as $post ) {
  $result[ ] = str_replace( ..... );
}

$html = implode( '', $result );

Vs this:
Code:
$result = '';

foreach ( $posts as $post ) {
  $result .= str_replace( ..... );
}
Try both and see which is faster. In some cases using array's is so much faster (10x or more) it will make you question what is going on under the hood.




Theme © iAndrew 2016 - Forum software by © MyBB