CodeIgniter - Load libraries if not already loaded

大兔子大兔子 提交于 2019-12-04 06:04:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!