框架资源消耗分析

作者 : admin 于 2008-12-03 22:42:09 标签: ,
2008
12-3

简单使用了下CI框架,发现了不少技巧:在 Controller类中实例化了好多的类

  1. function _ci_initialize()
  2. {
  3. // Assign all the class objects that were instantiated by the
  4. // front controller to local class variables so that CI can be
  5. // run as one big super object.
  6. $classes = array(
  7. 'config' => 'Config',
  8. 'input' => 'Input',
  9. 'benchmark' => 'Benchmark',
  10. 'uri' => 'URI',
  11. 'output' => 'Output',
  12. 'lang' => 'Language',
  13. 'router' => 'Router'
  14. );
  15. foreach ($classes as $var => $class)
  16. {
  17. $this->$var =& load_class($class);
  18. }
  19.  
  20. // In PHP 5 the Loader class is run as a discreet
  21. // class.  In PHP 4 it extends the Controller
  22. if (floor(phpversion()) >= 5)
  23. {
  24. $this->load =& load_class('Loader');
  25. $this->load->_ci_autoloader();
  26. }
  27. else
  28. {
  29. $this->_ci_autoloader();
  30. // sync up the objects since PHP4 was working from a copy
  31. foreach (array_keys(get_object_vars($this)) as $attribute)
  32. {
  33. if (is_object($this->$attribute))
  34. {
  35. $this->load->$attribute =& $this->$attribute;
  36. }
  37. }
  38. }

然后在默认welcome的模型里print_r($this),那是一个密密麻麻阿,无数的实例化数据摆在这里,估计内存CPU消耗也是惊人。
当然,作者并不是没有解决这些问题,所有的类在实例化之后使用static的方式常驻内存中,在第一次加载后,响应时间明显缩短,我本地测试是否默认实例化如此多的对象速度没有明显的差别。
对于原来我写程序,其实是有洁癖的,或者是心理障碍,就是像写C一样仔细考虑每个变量和每个方法,计算操作复杂度,其实对于整个系统开发和PHP这们语言来说,这么开发其实是杞人忧天,因为PHP本身就可以处理这些问题,比如static方法,一些缓存。
在进行各种测试之后,我的类库居然也默认加载了一些方法,只是应用在小网站中。
对于大型网站,还是要充分考虑效率性能的问题。

发表评论




XHTML:你可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(若看不到验证码,请重新加载页面。)