CodeIgniter Performance load view multiple times vs loop in view CodeIgniter Performance load view multiple times vs loop in view codeigniter codeigniter

CodeIgniter Performance load view multiple times vs loop in view


Well, I broke down and wrote a small script to inflate my table for these testing purposes. Here are the results of my small test. To keep the tests equivalent, I only change which view I am displaying.

Scenario 1

Controller:

public function results(){    $this->load->view('headers/header', $this->default_lib->viewdata());    $this->load->view('body_bits/bodyopen');    $this->title['title'] = 'Testing Tesults';    $this->title['link'] = 'tests';    $this->title['link_text'] = 'Back to Tests';    $this->load->view('body_bits/page_title', $this->title);    $this->load->view('tests/results_open');    $results = $this->db->select('*')->        from('test_results')->        where('software', 'nwralpha')->        order_by('time', 'ASC')->        //limit('10')->        get()->result_array();    foreach ($results as $key)    {        $this->data['name'] = ucwords($this->ion_auth->user($key['user_id'])->row()->username);        $this->data['time_taken'] = $key['time'];        $this->data['test_taken'] = $key['test_type'];        $this->data['common'] = ($key['common_codes'] == 1) ? 'Common Code List' : 'Full Code List';        $this->data['date'] = $key['date'];        $this->load->view('tests/results', $this->data);    }    $this->load->view('tests/results_close');    $this->load->view('body_bits/bodyclose');    $this->load->view('footers/footer');}

View

<tr>    <td><?php echo $name; ?></td>    <td><?php echo $time_taken; ?></td>    <td><?php echo $test_taken; ?></td>    <td><?php echo $common; ?></td>    <td><?php echo $date; ?></td></tr>

Scenario 2

Controller:

public function results(){    $this->load->view('headers/header', $this->default_lib->viewdata());    $this->load->view('body_bits/bodyopen');    $this->title['title'] = 'Testing Tesults';    $this->title['link'] = 'tests';    $this->title['link_text'] = 'Back to Tests';    $this->load->view('body_bits/page_title', $this->title);    $this->load->view('tests/results_open');    $results = $this->db->select('*')->        from('test_results')->        where('software', 'nwralpha')->        order_by('time', 'ASC')->        //limit('10')->        get()->result_array();    $this->data['results'] = $results;    $this->load->view('tests/resultsloop', $this->data);    $this->load->view('tests/results_close');    $this->load->view('body_bits/bodyclose');    $this->load->view('footers/footer');}

View

<?php foreach ($results as $key): ?><tr>    <td><?php echo ucwords($this->ion_auth->user($key['user_id'])->row()->username); ?></td>    <td><?php echo $key['time']; ?></td>    <td><?php echo $key['test_type']; ?></td>    <td><?php echo $key['common_codes']; ?></td>    <td><?php echo $key['date']; ?></td></tr><?php endforeach; ?>

Results

I tested both examples with 1004 results and with 11004 results and here is what I found using CodeIgniter's built in Profiler

1004 Results

Scenario 1 Average Load Time: 1.94462 Seconds
Scenario 2 Average Load Time: 1.1723 Seconds

11004 Results

Scenario 1 Average Load Time: 19.78867 Seconds
Scenario 2 Average Load Time: 11.81502 Seconds

What This Means

For anyone else who just wants to know the answer and not read this whole post:
While Separation of logic and HTML is the point of MVC frameworks, There are acceptable cases where you can use logic to determine how to display information. In this case, I only used a loop to keep spitting out uniform information as needed. This test shows that it is more efficient to make the least amount of view calls.


imho the controller hands off the relevant assets -- to the correct view. for example if you did not get any results back -- the course of action for a "no results" condition - should happen in the controller. (versus checking to see if $results exists in your view)

The logic for -- How do we Display the Results? -- happens in the View. The controller passes $results, the View then is responsible for how those results are displayed. With scenario #1, you have to go into your controller, to make changes to the page design.

And when $results are too large.... then use pagination.