-
Notifications
You must be signed in to change notification settings - Fork 45.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关于字符串拼接及优化的疑问 #2442
Comments
补充实验使用JMH进一步实验,在循环拼接只有2的情况下就有了很明显的落后,更不要说循环次数较多情况(几乎是线性关系的时间增加),所以在循环拼接的时候仍然应该使用string builder。 循环拼接:Benchmark Mode Cnt Score Error Units 构造string builder append拼接Benchmark Mode Cnt Score Error Units |
感谢分享,这样总结会不会更合适一些呢? 在 JDK 9 中,字符串相加“+”改为用动态方法 |
嗯 对的 我认为是这样的 |
我觉得这一部分描述存在歧义,java9优化中解决的是A+B+C的字符串拼接使用的相当于提前分配空间,但实际上对于循环的拼接依旧是两个两个动态分配内存,其实和两个两个append是一个概念(因为两个拼接不会出现额外需要扩容的情况)?肯定不如自己去string builder 然后append。
以下字节码使用的java17编译的:
The text was updated successfully, but these errors were encountered: