guoyaxu
posts - 1, comments - 0, trackbacks - 0, articles - 1
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
[导入]开启一个新的问题(关于声明变量的性能问题)
Posted on 2007-02-11 03:26
江海鱼
阅读(50)
评论(0)
编辑
收藏
在我们编程的过程中经常会遇到这样的问题。
for
(
int
i
=
0
;i
<
n;i
++
)
{
String str
=
//
}
String str
=
null
;
for
(
int
i
=
0
;i
<
n;i
++
)
{
str
=
//
}
在印象中一直认为方法二的性能好于方法一,但是差距应该很小。但因为一位别人的回文说方法一极大的影响了性能,所以想写个例子证明一下相差很小。例子如下:
public
class
TestOt
{
public
static
void
main(String[] args)
{
long
n
=
1000000000
;
long
start
=
System.currentTimeMillis();
test2(n);
long
end
=
System.currentTimeMillis();
System.out.println(end
-
start);
}
public
static
void
test1(
long
n)
{
for
(
int
i
=
0
;i
<
n;i
++
)
{
String str
=
""
;
}
}
public
static
void
test2(
long
n)
{
String str
=
null
;
for
(
int
i
=
0
;i
<
n;i
++
)
{
str
=
""
;
}
}
}
测试的结果是当n=10亿次的时候差距是1秒,所以说差距应该是很小的,符合我原始的记忆,但是另一个问题来了,测试的结果是
方法一:3300毫秒左右
方法二:4300毫秒左右
结果刚好相反,于是更改方法
public
class
TestOt
{
public
static
void
main(String[] args)
{
long
n
=
1000000000
;
long
start
=
System.currentTimeMillis();
test1(n);
long
end
=
System.currentTimeMillis();
System.out.println(end
-
start);
}
public
static
void
test1(
long
n)
{
for
(
int
i
=
0
;i
<
n;i
++
)
{
String str
=
null
;
}
}
public
static
void
test2(
long
n)
{
String str
=
null
;
for
(
int
i
=
0
;i
<
n;i
++
)
{
}
}
}
结果依旧。
没办法,取得字节码,对比
public
class
TestOt
extends
java.lang.Object
{
public
TestOt();
Code:
0
: aload_0
1
: invokespecial #
8
;
//
Method java/lang/Object."<init>":()V
4
:
return
public
static
void
test1(
int
);
Code:
0
: iconst_0
1
: istore_1
2
:
goto
10
5
: aconst_null
6
: astore_2
7
: iinc
1
,
1
10
: iload_1
11
: iload_0
12
: if_icmplt
5
15
:
return
public
static
void
test2(
int
);
Code:
0
: aconst_null
1
: astore_1
2
: iconst_0
3
: istore_2
4
:
goto
10
7
: iinc
2
,
1
10
: iload_2
11
: iload_0
12
: if_icmplt
7
15
:
return
}
结果是感觉还是应该是方法二快,那为什么反而方法一快了1秒左右呢?
不得而知,现在我个人猜测的想法是可能有两种情况:
1,JLS的底层定义决定的,有什么特殊的优化?
2,因为方法二比方法一虽然少了在循环中的部分,但是引用的声明周期反而是更长了,是否因为引用存在造成了方法二的栈操作消耗了大部分时间?
猜想一有待于JLS文档的查阅,我会在有空的时候查询,猜想二正在想办法证明。
看文章的朋友,如果谁了解麻烦指点一下,是我的测试方法写的有问题,还是别的原因,谢谢。
dreamstone
2007-02-11 03:26
发表评论
文章来源:
http://www.blogjava.net/dreamstone/archive/2007/02/11/99207.html
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
BlogJava
Copyright © 江海鱼
日历
<
2025年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
常用链接
我的随笔
我的评论
我的参与
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
java
javascript(1)
随笔档案
2007年3月 (1)
文章分类
javascript(1)
文章档案
2007年3月 (1)
新闻档案
2007年2月 (5)
相册
myphoto
收藏夹
Oracle(2)
搜索
最新评论