CodeIgniter Forums

Full Version: Performance: Loading many (smaller) views Vs fewer (larger) views
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]dynZack[/eluser]
I like to keep blocks of code in separate view files and load them as needed in the controller.

Like for example breadcrumbs.php , toplinks.php, searchbar.php, userpanel.php, etc.

And then, for example, have in the controller:

Code:
$main_data['header']=$this->load->view('include/header', $data, true);
$main_data['top_links']=$this->load->view('include/top_links', $data, true);
$main_data['leftbar']=$this->load->view('include/leftbar', $data, true);
$main_data['footer']=$this->load->view('include/footer', $data, true);
$main_data['breadcrumbs']=$this->load->view('include/footer', $data, true);
...
$this->load->vars($main_data);
$this->load->view('include/template');

Is there such thing as "too many include files"? Does the performance degrade with increasing $this->load->view() ?

El Forum

[eluser]slowgary[/eluser]
I'm sorry to see that no one responded to this reasonable question. I haven't done any benchmarking, but I can assure you that there is additional overhead for each view loaded. It could be very small, but at some point the view() function has to include() or require() the file in question, and loading files from disk does take time.

Additionally, view() in itself constitutes a function call, and I'm sure there's some logic involved in that call. Again, the amount of additional overhead could be very small, but I'm sure there's less overhead loading less views.

You could always throw a $this->load->view() call into a loop and do some testing. I usually break my views up into something like a header, body and footer.

El Forum

[eluser]sprise[/eluser]
I did a small test with 1-9 views loaded, first calling 3 views 3x. Load time didn't change.

Then I called 9 separate views, it did take an extra tenth of a second (this is on my local and no caching).

My best guess would be that it's not so much the calling of the load function, but just the cumulative amount of data to be processed.

ETA: I also break out into Header, footer, content. I wouldn't think twice about adding a few more pieces to it (like you have with Toplinks).

El Forum

[eluser]slowgary[/eluser]
I played around a bit as well. I created 100 blank views, then ran the following code with and without the commented break.
Code:
class Test extends Controller
{
    function index()
    {
        $this->output->enable_profiler(TRUE);

        $test = array(
            'test10' => 'apples',
            'test11' => 'apples',
            'test12' => 'apples',
            'test13' => 'apples',
            'test14' => 'apples',
            'test15' => 'apples',
            'test16' => 'apples',
            'test17' => 'apples',
            'test18' => 'apples',
            'test19' => 'apples'
        );

        for($i = 0; $i < 100; $i++)
        {
            $this->load->view('test/test'.$i, $test);
            // break;
        }
    }
}

The total execution time is below:
0.0155 - 0.0160 for one loop
0.0235 - 0.0240 for 100 loops

So it looks like each view() call is taking less than 1 thousandth of a second. Obviously the amount of time will depend on how fast your server is, but I think in your case dynZack, the extra calls are moot.

I hope this helps.

El Forum

[eluser]WanWizard[/eluser]
I find that the more I can break up an application into logical pieces, to easier it is to maintain.

If, during the lifespan of the application, this can save you one or two days of work, you've saved enough to buy a server with a slightly higher spec. Whichs negates the slightly slower application.

Experience shows that it saves a lot more than that, if we're talking about a reasonable size application...

El Forum

[eluser]dynZack[/eluser]
[quote author="slowgary" date="1283827078"]I played around a bit as well. I created 100 blank views, then ran the following code with and without the commented break.
Code:
class Test extends Controller
{
    function index()
    {
        $this->output->enable_profiler(TRUE);

        $test = array(
            'test10' => 'apples',
            'test11' => 'apples',
            'test12' => 'apples',
            'test13' => 'apples',
            'test14' => 'apples',
            'test15' => 'apples',
            'test16' => 'apples',
            'test17' => 'apples',
            'test18' => 'apples',
            'test19' => 'apples'
        );

        for($i = 0; $i < 100; $i++)
        {
            $this->load->view('test/test'.$i, $test);
            // break;
        }
    }
}

The total execution time is below:
0.0155 - 0.0160 for one loop
0.0235 - 0.0240 for 100 loops

So it looks like each view() call is taking less than 1 thousandth of a second. Obviously the amount of time will depend on how fast your server is, but I think in your case dynZack, the extra calls are moot.

I hope this helps.[/quote]


Thanks for that. Really helpful