列出具有预定义大小或没有大小的varargs [英] List to varargs with pre-difined size or without size
问题描述
将List转换为varargs以便在"builder(String ... s)"方法下面调用的更好方法是什么.
builder(stringList.toArray(new String [stringList.size()])));//具有预先定义的数组大小
或
builder(stringList.toArray(new String [0]));//没有预先确定的大小
例如
void test(){List< String>stringList = new ArrayList<>();builder(stringList.toArray(new String [stringList.size()]));//调用具有预定义数组大小的生成器方法builder(stringList.toArray(new String [0]));//调用数组大小为0的builder方法}无效的builder(String ... s){}
我在评论中遇到了这个问题,并建议我 builder(stringList.toArray(new String [0]))
比使用 builder(stringList.toArray(new String [stringList.size()]))
.
两者之间有显着差异吗?谢谢
据我了解
stringList.toArray(new String [stringList.size()]))
更有效率.原因:
该参数需要具有通用 List< String>
的实际类型( String
),其中在运行时会删除该通用类型参数./p>
如果参数的大小与列表大小匹配,则将其用于结果数组.
如果大小为0,则丢弃传递的数组.
因此,传递正确的数组可以节省一个对象的创建.
当然, list.size()
被称为多余的.因此,它可能会更慢.我对此表示怀疑.
更正
请参见古代智慧的阵列.正确的基准表明了相反的情况: new String [0]
更快.我只是跳过了非常有趣的分析,而且看起来:
- (多余的
new String [0]
无关紧要;) - 在toArray方法中进行本地数组复制可以实现不同的,更快的数组复制;
- (然后是对
size
的额外调用.)
注意,我没有足够详尽地阅读过这篇文章;真的很有趣.
结论(与直觉相反): 新T [0]
更快.
记住:
- 代码检查器可能仍会有所不同并发出警告;
- 这是在热身:在热点JIT出现之前,情况可能恰恰相反.
What is the better way to convert List to varargs to call below 'builder( String... s )' method.
builder( stringList.toArray( new String[stringList.size()] ) );//with pre-difined array size
or
builder( stringList.toArray( new String[0] ) );//without predifined size
for example
void test() {
List<String> stringList = new ArrayList<>();
builder( stringList.toArray( new String[stringList.size()] ) );//call builder method with pre-difining array size
builder( stringList.toArray( new String[0] ) );//call builder method with array size 0
}
void builder( String... s )
{
}
I faced this question in a review and I was suggested that builder( stringList.toArray( new String[0] ) )
is more efficient than
using builder( stringList.toArray( new String[stringList.size()] ) )
.
Is there a significant difference between these two?
Thanks
To my understanding
stringList.toArray( new String[stringList.size()] ) )
is more efficient. The reason:
The argument is needed to have an actual type (String
) for a generic List<String>
, where the generic type parameter is erased at run-time.
The argument is used for the resulting array if its size matches the list size.
If the size is 0, the passed array is discarded.
So passing a correct array saves one object creation.
Of course list.size()
is called extra. So it might be slower. I doubt it.
Correction
See Arrays of Wisdom of the Ancients.
A correct benchmark shows the inverse: new String[0]
being faster.
I just overflew the very interesting analysis, and it seems:
- (an extra short lived
new String[0]
is irrelevant;) - doing the array copying local in the toArray method allows a different, faster array copy;
- (and then there is the extra call to
size
.)
Mind, I did not sufficiently thorough read the article; it really is interesting.
Conclusion (counter-intuitively): new T[0]
is faster.
Mind that:
- code checkers might still think differently and issue a warning;
- this is with warming up: till the hotspot JIT kicks in, it may be the other way around.
这篇关于列出具有预定义大小或没有大小的varargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!