您的位置:首页 > 编程语言 > Java开发

java final方法执行效率测试

2010-05-03 12:22 459 查看
在使用final修饰方法的时候,编译器会将被final修饰过的方法插入到调用者代码处,提高运行速度和效率,但被final修饰的方法体不能过大,编译器可能会放弃内联,但究竟多大的方法会放弃,我还没有做测试来计算过。

本文是通过两个疑问来继续阐述的:

1、使用final修饰方法会提高速度和效率吗;
见下面的测试代码,我会执行五次:

Java代码


public class Test

{

public static void getJava()

{

String str1 = "Java ";

String str2 = "final ";

for (int i = 0; i < 10000; i++)

{

str1 += str2;

}

}

public static final void getJava_Final()

{

String str1 = "Java ";

String str2 = "final ";

for (int i = 0; i < 10000; i++)

{

str1 += str2;

}

}

public static void main(String[] args)

{

long start = System.currentTimeMillis();

getJava();

System.out.println("调用不带final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");

start = System.currentTimeMillis();

String str1 = "Java ";

String str2 = "final ";

for (int i = 0; i < 10000; i++)

{

str1 += str2;

}

System.out.println("正常的执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");

start = System.currentTimeMillis();

getJava_Final();

System.out.println("调用final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");

}

}

public class Test
{
public static void getJava()
{
String str1 = "Java ";
String str2 = "final ";
for (int i = 0; i < 10000; i++)
{
str1 += str2;
}
}
public static final void getJava_Final()
{
String str1 = "Java ";
String str2 = "final ";
for (int i = 0; i < 10000; i++)
{
str1 += str2;
}
}
public static void main(String[] args)
{
long start = System.currentTimeMillis();
getJava();
System.out.println("调用不带final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");
start = System.currentTimeMillis();
String str1 = "Java ";
String str2 = "final ";
for (int i = 0; i < 10000; i++)
{
str1 += str2;
}
System.out.println("正常的执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");
start = System.currentTimeMillis();
getJava_Final();
System.out.println("调用final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");
}
}


结果为:
第一次:
调用不带final修饰的方法执行时间为:1732毫秒时间
正常的执行时间为:1498毫秒时间
调用final修饰的方法执行时间为:1593毫秒时间
第二次:
调用不带final修饰的方法执行时间为:1217毫秒时间
正常的执行时间为:1031毫秒时间
调用final修饰的方法执行时间为:1124毫秒时间
第三次:
调用不带final修饰的方法执行时间为:1154毫秒时间
正常的执行时间为:1140毫秒时间
调用final修饰的方法执行时间为:1202毫秒时间
第四次:
调用不带final修饰的方法执行时间为:1139毫秒时间
正常的执行时间为:999毫秒时间
调用final修饰的方法执行时间为:1092毫秒时间
第五次:
调用不带final修饰的方法执行时间为:1186毫秒时间
正常的执行时间为:1030毫秒时间
调用final修饰的方法执行时间为:1109毫秒时间

由以上运行结果不难看出,执行最快的是“正常的执行”即代码直接编写,而使用final修饰的方法,不像有些书上或者文章上所说的那样,速度与效率与“正常的执行”无异,而是位于第二位,最差的是调用不加final修饰的方法。
观点:加了比不加好一点。

1、使用final修饰变量会让变量的值不能被改变吗;
见代码:

Java代码


public class Final

{

public static void main(String[] args)

{

Color.color[3] = "white";

for (String color : Color.color)

System.out.print(color+" ");

}

}

class Color

{

public static final String[] color = { "red", "blue", "yellow", "black" };

}

public class Final
{
public static void main(String[] args)
{
Color.color[3] = "white";
for (String color : Color.color)
System.out.print(color+" ");
}
}

class Color
{
public static final String[] color = { "red", "blue", "yellow", "black" };
}


执行结果:
red blue yellow white
看!,黑色变成了白色。
在使用findbugs插件时,就会提示public static String[] color = { "red", "blue", "yellow", "black" };这行代码不安全,但加上final修饰,这行代码仍然是不安全的,因为final没有做到保证变量的值不会被修改!原因是:final关键字只能保证变量本身不能被赋与新值,而不能保证变量的内部结构不被修改。例如在main方法有如下代码Color.color = new String[]{""};就会报错了。那可能有的同学就会问了,加上final关键字不能保证数组不会被外部修改,那有什么方法能够保证呢?答案就是降低访问级别,把数组设为private。这样的话,就解决了数组在外部被修改的不安全性,但也产生了另一个问题,那就是这个数组要被外部使用的。
解决这个问题见代码:

Java代码


import java.util.AbstractList;

import java.util.List;

public class Final

{

public static void main(String[] args)

{

for (String color : Color.color)

System.out.print(color + " ");

Color.color.set(3, "white");

}

}

class Color

{

private static String[] _color = { "red", "blue", "yellow", "black" };

public static List<String> color = new AbstractList<String>()

{

@Override

public String get(int index)

{

return _color[index];

}

@Override

public String set(int index, String value)

{

throw new RuntimeException("为了代码安全,不能修改数组");

}

@Override

public int size()

{

return _color.length;

}

};

import java.util.AbstractList;
import java.util.List;

public class Final
{
public static void main(String[] args)
{
for (String color : Color.color)
System.out.print(color + " ");
Color.color.set(3, "white");
}
}

class Color
{
private static String[] _color = { "red", "blue", "yellow", "black" };
public static List<String> color = new AbstractList<String>()
{
@Override
public String get(int index)
{
return _color[index];
}
@Override
public String set(int index, String value)
{
throw new RuntimeException("为了代码安全,不能修改数组");
}
@Override
public int size()
{
return _color.length;
}
};


}这样就OK了,既保证了代码安全,又能让数组中的元素被访问了。

本文来自:http://songhongchen.javaeye.com/blog/361685
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: