java2e_java2e performance guides的一点疑问

String variables[] = new String[50000];

for( int i=0;i

variables[i] = "s"+i;

}

// create String literals

long startTime0 = System.currentTimeMillis();

for(int i=0;i

variables[i] = "hello";

}

long endTime0 = System.currentTimeMillis();

System.out.println("Time taken for creation of String literals : "

+ (endTime0 - startTime0) + " milli seconds" );

// create String objects using 'new' keyword

long startTime1 = System.currentTimeMillis();

for(int i=0;i

variables[i] = new String("hello");

}

long endTime1 = System.currentTimeMillis();

System.out.println("Time taken for creation of String objects with 'new' key word : "

+ (endTime1 - startTime1)+" milli seconds");

long startTime2 = System.currentTimeMillis();

for(int i=0;i

variables[i] = new String("hello");

variables[i] = variables[i].intern();

}

long endTime2 = System.currentTimeMillis();

System.out.println("Time taken for creation of String objects with intern(): "

+ (endTime2 - startTime2)+" milli seconds");

这段代码在文章中给出的结果为:

Time taken for creation of String literals : 0 milli seconds

Time taken for creation of String objects with 'new' key word : 160 milli seconds

Time taken for creation of String objects with intern(): 60 milli seconds

但是我在电脑上实际运行结果为:

Time taken for creation of String literals : 0 milli seconds

Time taken for creation of String objects with 'new' key word : 3 milli seconds

Time taken for creation of String objects with intern(): 7 milli seconds

刨除硬件运行速度差异,明显区别还是String.intern()执行到底有没有带来效率上的提高.似乎我的运行结果与作者要说明的事实恰恰相反.

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-03-06 14:00

浏览 661

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值