【编程语言】避免创建不必要的对象

编程时重用最好能重用对象,而不是每次需要的时候都去创建一个同样功能的新对象,如果对象是不可变的,则它可以始终被重用
除了重用那些不可变的对象外,也可以重用那些已知的不会被修改的可变对象,下面举一个常见的例子说明减少创建对象对代码优化
的重要性

模型说明:
其中涉及了可变的Date对象,它的值一旦计算出来就不在改变,下面建立一个模型:其中一个人,并有IsPost90s方法,判断此人是否为90后
模型代码:
代码片段一:


1. <span >/**</span>
2. <span > * 需求:判断此人是否属于属于90后,即出生年在1990年至1999年之间</span>
3. <span > * @author RUJIA_LI</span>
4. <span > *</span>
5. <span > */</span>
6. <span >public</span><span > </span><span >class</span><span > </span><span >Person1</span><span > </span><span >{</span>
7. <span > </span><span >private</span><span > </span><span >String</span><span > name</span><span >;</span>
8. <span > </span><span >private</span><span > </span><span >Date</span><span > birthDate</span><span >;</span>
9. <span > </span><span >public</span><span > </span><span >Person1</span><span > </span><span >(</span><span >String</span><span > name</span><span >,</span><span > </span><span >Date</span><span > birthDate </span><span >)</span><span > </span><span >{</span>
10. <span > </span><span >this</span><span >.</span><span >name </span><span >=</span><span > name</span><span >;</span>
11. <span > </span><span >this</span><span >.</span><span >birthDate </span><span >=</span><span > birthDate</span><span >;</span>
12. <span > </span><span >}</span>
13. <span > </span><span >public</span><span > </span><span >boolean</span><span > </span><span >IsPost90s</span><span >(){</span>
14. <span > </span><span >Calendar</span><span > cal </span><span >=</span><span > </span><span >Calendar</span><span >.</span><span >getInstance</span><span >(</span><span >TimeZone</span><span >.</span><span >getTimeZone</span><span >(</span><span >"GMT"</span><span >));</span>
15. <span > cal</span><span >.</span><span >set</span><span >(</span><span >1990</span><span >,</span><span > </span><span >Calendar</span><span >.</span><span >JANUARY</span><span >,</span><span > </span><span >1</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >);</span>
16. <span > </span><span >Date</span><span > startdate </span><span >=</span><span > cal</span><span >.</span><span >getTime</span><span >();</span>
17. <span > cal</span><span >.</span><span >set</span><span >(</span><span >1999</span><span >,</span><span > </span><span >Calendar</span><span >.</span><span >JANUARY</span><span >,</span><span > </span><span >1</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >);</span>
18. <span > </span><span >Date</span><span > enddate </span><span >=</span><span > cal</span><span >.</span><span >getTime</span><span >();</span>
19. <span > </span><span >return</span><span > birthDate</span><span >.</span><span >compareTo</span><span >(</span><span >startdate</span><span >)&gt;=</span><span >0</span><span >&amp;&amp;</span><span >birthDate</span><span >.</span><span >compareTo</span><span >(</span><span >enddate</span><span >)&lt;</span><span >0</span><span >;</span>
20. <span > </span><span >}</span>
21. <span >}</span>
IsPost90s()方法每次被调用的时候,都会新建一个Calendar对象,一个TimeZone对象和两个Date对象,这是非常没有意义的
下面的版本用了静态初始化器,
代码片段二:

  1. <span >/**</span>
  2. <span > * 需求:判断此人是否属于属于90后,即出生年在1990年至1999年之间</span>
  3. <span > * @author RUJIA_LI</span>
  4. <span > *</span>
  5. <span > */</span>
  6. <span >public</span><span > </span><span >class</span><span > </span><span >Person2</span><span > </span><span >{</span>
  7. <span > </span><span >private</span><span > </span><span >String</span><span > name</span><span >;</span>
  8. <span > </span><span >private</span><span > </span><span >Date</span><span > birthDate</span><span >;</span>
  9. <span > </span><span >private</span><span > </span><span >static</span><span > </span><span >final</span><span > </span><span >Date</span><span > START_TIME</span><span >;</span>
  10. <span > </span><span >private</span><span > </span><span >static</span><span > </span><span >final</span><span > </span><span >Date</span><span > END_TIME</span><span >;</span>
  11. <span > </span>
  12. <span > </span><span >public</span><span > </span><span >Person2</span><span >(</span><span >String</span><span > name</span><span >,</span><span > </span><span >Date</span><span > birthDate </span><span >)</span><span > </span><span >{</span>
  13. <span > </span><span >this</span><span >.</span><span >name </span><span >=</span><span > name</span><span >;</span>
  14. <span > </span><span >this</span><span >.</span><span >birthDate </span><span >=</span><span > birthDate</span><span >;</span>
  15. <span > </span><span >}</span>
  16. <span > </span><span >static</span><span >{</span>
  17. <span > </span><span >Calendar</span><span > cal </span><span >=</span><span > </span><span >Calendar</span><span >.</span><span >getInstance</span><span >(</span><span >TimeZone</span><span >.</span><span >getTimeZone</span><span >(</span><span >"GMT"</span><span >));</span>
  18. <span > cal</span><span >.</span><span >set</span><span >(</span><span >1990</span><span >,</span><span > </span><span >Calendar</span><span >.</span><span >JANUARY</span><span >,</span><span > </span><span >1</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >);</span>
  19. <span > START_TIME</span><span >=</span><span > cal</span><span >.</span><span >getTime</span><span >();</span>
  20. <span > cal</span><span >.</span><span >set</span><span >(</span><span >1999</span><span >,</span><span > </span><span >Calendar</span><span >.</span><span >JANUARY</span><span >,</span><span > </span><span >1</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >,</span><span > </span><span >0</span><span >);</span>
  21. <span > END_TIME </span><span >=</span><span > cal</span><span >.</span><span >getTime</span><span >();</span>
  22. <span > </span><span >}</span>
  23. <span > </span><span >public</span><span > </span><span >boolean</span><span > </span><span >IsPost90s</span><span >(){</span>
  24. <span > </span><span >return</span><span > birthDate</span><span >.</span><span >compareTo</span><span >(</span><span >START_TIME</span><span >)&gt;=</span><span >0</span><span >&amp;&amp;</span><span >birthDate</span><span >.</span><span >compareTo</span><span >(</span><span >END_TIME</span><span >)&lt;</span><span >0</span><span >;</span>
  25. <span > </span><span >}</span>
  26. <span >}</span>
    测试结构:
    Person1Person2中的IsPost90s方法分别被调用5000万次
    执行时间对比:

第一种方式耗时152161毫秒  

第二种方式耗时28761毫秒  

堆栈利用情况:
第一种方式堆栈使用情况

第一种方式堆栈使用情况


改进后的Person2类在仅在初始化创建Calendar、TimeZone和Date对象一次,而不是每次调用IsPost90s的时候创建这些实例,把START_TIME和
END_TIME从局部变量变为final静态域,当做常量处理,从上面的执行时间可以看出减少对象的创建可以大幅度提高性能,从堆栈的使用情况可知,
频繁的创建的对象加速的虚拟机的GC速度











 
your support will encourage me to continue to create!
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)