遍历字符串中所有字符的最快方法

在Java中,迭代字符串中所有字符的最快方法是:


String str = "a really, really long string";

for (int i = 0, n = str.length(); i < n; i++) {

    char c = str.charAt(i);

}

或这个:


char[] chars = str.toCharArray();

for (int i = 0, n = chars.length; i < n; i++) {

    char c = chars[i];

}

编辑:


我想知道的是,charAt在长时间的迭代过程中重复调用该方法的开销是否小于还是大于toCharArray在开始时执行一次单次调用然后在迭代过程中直接访问数组的开销。


如果有人可以针对不同的字符串长度提供可靠的基准测试,那将是一个很好的选择,不仅要考虑到JIT的预热时间,JVM的启动时间等,而且还不只是两次调用的区别System.currentTimeMillis()。


不负相思意
浏览 553回答 3
3回答

守着一只汪

这只是您不应该担心的微观优化。char[] chars = str.toCharArray();返回一个str字符数组的副本(在JDK中,它通过调用返回一个字符的副本System.arrayCopy)。除此之外,str.charAt()仅检查索引是否确实在范围内,并在数组索引内返回一个字符。第一个不会在JVM中创建额外的内存。

蛊毒传说

如果需要处理大量数据,则不应在客户端模式下使用JVM。客户端模式并非用于优化。让我们在客户端模式和服务器模式下使用JVM比较@Saint Hill基准测试的结果。Core2Quad Q6600 G0 @ 2.4GHzJavaSE 1.7.0_40另请参见:“ java -server”和“ java -client”之间的真正区别?客户模式:len =&nbsp; &nbsp; &nbsp; 2:&nbsp; &nbsp; 111k charAt(i),&nbsp; 105k cbuff[i],&nbsp; &nbsp;62k new[i],&nbsp; &nbsp;17k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 4:&nbsp; &nbsp; 285k charAt(i),&nbsp; 166k cbuff[i],&nbsp; 114k new[i],&nbsp; &nbsp;43k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 6:&nbsp; &nbsp; 315k charAt(i),&nbsp; 230k cbuff[i],&nbsp; 162k new[i],&nbsp; &nbsp;69k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 8:&nbsp; &nbsp; 333k charAt(i),&nbsp; 275k cbuff[i],&nbsp; 181k new[i],&nbsp; &nbsp;85k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;12:&nbsp; &nbsp; 342k charAt(i),&nbsp; 342k cbuff[i],&nbsp; 222k new[i],&nbsp; 117k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;16:&nbsp; &nbsp; 363k charAt(i),&nbsp; 347k cbuff[i],&nbsp; 275k new[i],&nbsp; 152k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;20:&nbsp; &nbsp; 363k charAt(i),&nbsp; 392k cbuff[i],&nbsp; 289k new[i],&nbsp; 180k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;24:&nbsp; &nbsp; 375k charAt(i),&nbsp; 428k cbuff[i],&nbsp; 311k new[i],&nbsp; 205k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;28:&nbsp; &nbsp; 378k charAt(i),&nbsp; 474k cbuff[i],&nbsp; 341k new[i],&nbsp; 233k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;32:&nbsp; &nbsp; 376k charAt(i),&nbsp; 492k cbuff[i],&nbsp; 340k new[i],&nbsp; 251k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;64:&nbsp; &nbsp; 374k charAt(i),&nbsp; 551k cbuff[i],&nbsp; 374k new[i],&nbsp; 367k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 128:&nbsp; &nbsp; 385k charAt(i),&nbsp; 624k cbuff[i],&nbsp; 415k new[i],&nbsp; 509k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 256:&nbsp; &nbsp; 390k charAt(i),&nbsp; 675k cbuff[i],&nbsp; 436k new[i],&nbsp; 619k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 512:&nbsp; &nbsp; 394k charAt(i),&nbsp; 703k cbuff[i],&nbsp; 439k new[i],&nbsp; 695k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;1024:&nbsp; &nbsp; 395k charAt(i),&nbsp; 718k cbuff[i],&nbsp; 462k new[i],&nbsp; 742k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;2048:&nbsp; &nbsp; 396k charAt(i),&nbsp; 725k cbuff[i],&nbsp; 471k new[i],&nbsp; 767k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;4096:&nbsp; &nbsp; 396k charAt(i),&nbsp; 727k cbuff[i],&nbsp; 459k new[i],&nbsp; 780k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;8192:&nbsp; &nbsp; 397k charAt(i),&nbsp; 712k cbuff[i],&nbsp; 446k new[i],&nbsp; 772k field access.&nbsp; &nbsp;(chars/ms)&nbsp;服务器模式:len =&nbsp; &nbsp; &nbsp; 2:&nbsp; &nbsp; &nbsp;86k charAt(i),&nbsp; &nbsp;41k cbuff[i],&nbsp; &nbsp;46k new[i],&nbsp; &nbsp;80k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 4:&nbsp; &nbsp; 571k charAt(i),&nbsp; 250k cbuff[i],&nbsp; &nbsp;97k new[i],&nbsp; 222k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 6:&nbsp; &nbsp; 666k charAt(i),&nbsp; 333k cbuff[i],&nbsp; 125k new[i],&nbsp; 315k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp; 8:&nbsp; &nbsp; 800k charAt(i),&nbsp; 400k cbuff[i],&nbsp; 181k new[i],&nbsp; 380k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;12:&nbsp; &nbsp; 800k charAt(i),&nbsp; 521k cbuff[i],&nbsp; 260k new[i],&nbsp; 545k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;16:&nbsp; &nbsp; 800k charAt(i),&nbsp; 592k cbuff[i],&nbsp; 296k new[i],&nbsp; 640k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;20:&nbsp; &nbsp; 800k charAt(i),&nbsp; 666k cbuff[i],&nbsp; 408k new[i],&nbsp; 800k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;24:&nbsp; &nbsp; 800k charAt(i),&nbsp; 705k cbuff[i],&nbsp; 452k new[i],&nbsp; 800k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;28:&nbsp; &nbsp; 777k charAt(i),&nbsp; 736k cbuff[i],&nbsp; 368k new[i],&nbsp; 933k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;32:&nbsp; &nbsp; 800k charAt(i),&nbsp; 780k cbuff[i],&nbsp; 571k new[i],&nbsp; 969k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; &nbsp;64:&nbsp; &nbsp; 800k charAt(i),&nbsp; 901k cbuff[i],&nbsp; 800k new[i],&nbsp; 1306k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 128:&nbsp; &nbsp; 1084k charAt(i),&nbsp; 888k cbuff[i],&nbsp; 633k new[i],&nbsp; 1620k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 256:&nbsp; &nbsp; 1122k charAt(i),&nbsp; 966k cbuff[i],&nbsp; 729k new[i],&nbsp; 1790k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp; 512:&nbsp; &nbsp; 1163k charAt(i),&nbsp; 1007k cbuff[i],&nbsp; 676k new[i],&nbsp; 1910k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;1024:&nbsp; &nbsp; 1179k charAt(i),&nbsp; 1027k cbuff[i],&nbsp; 698k new[i],&nbsp; 1954k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;2048:&nbsp; &nbsp; 1184k charAt(i),&nbsp; 1043k cbuff[i],&nbsp; 732k new[i],&nbsp; 2007k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;4096:&nbsp; &nbsp; 1188k charAt(i),&nbsp; 1049k cbuff[i],&nbsp; 742k new[i],&nbsp; 2031k field access.&nbsp; &nbsp;(chars/ms)&nbsp;len =&nbsp; &nbsp;8192:&nbsp; &nbsp; 1157k charAt(i),&nbsp; 1032k cbuff[i],&nbsp; 723k new[i],&nbsp; 2048k field access.&nbsp; &nbsp;(chars/ms)&nbsp;结论:如您所见,服务器模式要快得多。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java