框架资源消耗分析
12-3
简单使用了下CI框架,发现了不少技巧:在 Controller类中实例化了好多的类
- function _ci_initialize()
- {
- // Assign all the class objects that were instantiated by the
- // front controller to local class variables so that CI can be
- // run as one big super object.
- $classes = array(
- 'config' => 'Config',
- 'input' => 'Input',
- 'benchmark' => 'Benchmark',
- 'uri' => 'URI',
- 'output' => 'Output',
- 'lang' => 'Language',
- 'router' => 'Router'
- );
- foreach ($classes as $var => $class)
- {
- $this->$var =& load_class($class);
- }
- // In PHP 5 the Loader class is run as a discreet
- // class. In PHP 4 it extends the Controller
- if (floor(phpversion()) >= 5)
- {
- $this->load =& load_class('Loader');
- $this->load->_ci_autoloader();
- }
- else
- {
- $this->_ci_autoloader();
- // sync up the objects since PHP4 was working from a copy
- foreach (array_keys(get_object_vars($this)) as $attribute)
- {
- if (is_object($this->$attribute))
- {
- $this->load->$attribute =& $this->$attribute;
- }
- }
- }
然后在默认welcome的模型里print_r($this),那是一个密密麻麻阿,无数的实例化数据摆在这里,估计内存CPU消耗也是惊人。
当然,作者并不是没有解决这些问题,所有的类在实例化之后使用static的方式常驻内存中,在第一次加载后,响应时间明显缩短,我本地测试是否默认实例化如此多的对象速度没有明显的差别。
对于原来我写程序,其实是有洁癖的,或者是心理障碍,就是像写C一样仔细考虑每个变量和每个方法,计算操作复杂度,其实对于整个系统开发和PHP这们语言来说,这么开发其实是杞人忧天,因为PHP本身就可以处理这些问题,比如static方法,一些缓存。
在进行各种测试之后,我的类库居然也默认加载了一些方法,只是应用在小网站中。
对于大型网站,还是要充分考虑效率性能的问题。