String.valueOf(i)vs"" + i或i +“” [英] String.valueOf(i) vs "" + i or i + ""

查看:220
本文介绍了String.valueOf(i)vs"" + i或i +“”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用+ i 以方便。但是比较一下我自己的性能,我认为 String.valueOf(i)会更快。这样对吗?我应该使用哪一个?

I usually use "" + i for convenient. But compare about perfomance myself, I thought String.valueOf(i) will be faster. Is it right? Which one should I use?

更新:我已经阅读了你的答案,这是我能说的:

- 只有很小的性能差异如果代码在递归块内,则变大。
- 你没有告诉我这两种情况的主要和基本缺点/优点究竟是什么。

UPDATE: I've read your answers, here is what I can tell:
- Only small performance difference will become big if that code is inside a recursive block.
- You didn't tell me exactly what is main and basic disadvantage/advantage of both case.

推荐答案

选择最佳读物。然后,在优化任何内容之前对应用程序进行概要分析。

Choose what reads best. Then, profile your application before optimizing anything.

始终首先进行概要分析(例如,使用 VisualVM ,因为您使用Java进行编码。)

Always profile first (for instance with VisualVM since you're coding in Java).

有很多关于分析和优化的资源。我遇到的最新读物是@ href=\"http://altdevblogaday.com/2011/05/28/step_by_step_optimisation/\" rel=\"nofollow\"> Tony Albrecht的逐步优化。

There are many resources on profiling and optimization. The latest good read I came across is this Step by Step Optimisation by Tony Albrecht.

如果性能真的变得很重要,请考虑切换到 StringBuilder

And if performance really becomes critical, consider switching to StringBuilder.

编辑:

那么你的问题就没有直接答案了。微基准测试 String.valueOf(i) i +的表现几乎没有价值。真正的答案来自对您的应用程序进行基准测试:它自身的复杂性和内存访问模式决定了这一切。

Well there is no straight answer to your question imho. Micro-benchmarking the performance of String.valueOf(i) against i + "" has little value. The real answer comes from benchmarking your application: its very own complexity and memory access pattern decides it all.

这些不是我的想法,但我正在购买它们:停止编程,就像1975年一样大O符号需要更新

Those are not my ideas but I'm buying them: stop programming like it's 1975, big O notation needs an update.

这篇关于String.valueOf(i)vs"" + i或i +“”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆