问题描述
(这个帖子是关于高频型编程)
我最近在一个论坛上(我认为他们在讨论的Java)看到,如果你要分析很多字符串数据它能够更好地使用一个字节数组比分割字符串()。确切的职位是:
Is he just saying "dont use strings because they're an object and creating objects is costly" ? Or is he saying something else?
Does using a byte array ensure the data remains in the cache for as long as possible?When you use a string is it too large to be held in the CPU cache?Generally, is using the primitive data types the best methods for writing faster code?
He's saying that if you break a chunk text up into separate string objects, those string objects have worse locality than the large array of text. Each string, and the array of characters it contains, is going to be somewhere else in memory; they can be spread all over the place. It is likely that the memory cache will have to thrash in and out to access the various strings as you process the data. In contrast, the one large array has the best possible locality, as all the data is on one area of memory, and cache-thrashing will be kept to a minimum.
There are limits to this, of course: if the text is very, very large, and you only need to parse out part of it, then those few small strings might fit better in the cache than the large chunk of text.
这篇关于字符串VS字节数组,性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!