6

What I'm trying to do is to load libraries if they not already loaded (either CI's libraries, either custom ones) on many different points of a website. So I want to do a check on this.

I have done a search on Loader library and found the is_loaded() function, so I could do this for example:

if ($this->load->is_loaded('form_validation') === false) {
    $this->load->library('form_validation');            
}

The strange thing with this (with profiler enabled) is that the memory goes up, which makes me wondering wether this is the correct way or not.

9
  • 6
    You should just use $this->load->library('library'), the check is done automatically, and the library won't be loaded if it's already Oct 28, 2013 at 8:27
  • 1
    Then why the memory increases???
    – ltdev
    Oct 28, 2013 at 8:28
  • 2
    Don't know, but still: just use $this->load->library(), without any check whatsoever Oct 28, 2013 at 8:30
  • Does the same thing also applies and with helpers???
    – ltdev
    Oct 28, 2013 at 8:36
  • 2
    @Lykos The memory goes up because the is_loaded() method will directly return the library object if it is already loaded.
    – Narf
    Oct 28, 2013 at 9:28

1 Answer 1

19

Around line 914 in system/core/Loader.php, The Codeigniter perform check if the library is loaded and it will not load it again. However, these checks consume some memory as well. To conclude which way is the best for loading libraries, I did a little benchmark (cleaning memory after each attempt) and the conclusion here is:

Just load the library normally with $this->load... and let the Codeigniter handle it


Benchmark:

$this->load->library('session');

After initial load of Codeigniter session class, I tested various ways loading library and/or performing check if it's not loaded already. Each of these lines were executed separately for 20x times:


MEMORY CONSUMPTION TEST (Not speed!)

if(!$this->load->is_loaded('session')) $this->load->library('session');

This consumed 48.256 bytes


if(!class_exists('ci_session')) $this->load->library('session');

This consumed 39.824bytes


if(!isset($this->session)) $this->load->library('session');

This consumed 31.904bytes


$this->load->library('session');

This consumed 21.790bytes


After repeating the test for one more time, the results were the same, so I guess it might just be relevant! Please comment if I'm wrong!


07.08.2014. UPDATE using Codeigniter 2.2.0: The test was repeated using 1000 iterations (not 20 like before). The results remain the same. Memory consumption was as follows: 2128b, 1856b, 1688b, 1456b

@Tim Dev notes in the comment that this benchmark doesn't necessary shows the fastest code but only lowest memory consumption code.

5
  • 1
    Thanks for your thorough and helpful reply!
    – ltdev
    May 12, 2014 at 6:58
  • I had the same problem and realised I should share this with others May 12, 2014 at 14:47
  • 3
    Just benchmarked this by reloading a a method 100 times which tries to load the same library. !class_exists() 1.95 milliseconds, !isset() 5.6milliseconds, !is_loaded() 39 milliseconds, and no checks but just trust on codeigniter checks is 300+milliseconds. So class_exists is faster.
    – Timmetje
    Jul 10, 2014 at 14:34
  • As I've made now a 1000 iterations test I've realized that we are not at the same lane here. I'm talking about memory consumption while you were bench marking elapsed time. This could have been discussed further on how these tests should be executed. The elapsed time results are though logical as Codeigniter code would have much more lines to execute than native class_exists(). If you ask me, I think reducing memory consuption would be much better practice then saving a few hundred milliseconds of time, but it's just my opionion Aug 7, 2014 at 13:12
  • @intsoumen No waaaay? Too bad that this is a memory consumption test, not the speed performance test Aug 23, 2017 at 20:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.