list.clear()vs list = new ArrayList ();(list.clear() vs list = new ArrayList(); [duplicate])
这个问题已经在这里有一个答案:
- 清空一个ArrayList或者只是创建一个新的,让旧的被垃圾收集? [复制] 4个答案
- Map.clear()vs new Map:哪一个会更好? [复制] 7个答案
清除ArrayList的2个选项中的哪一个更好,更快,为什么?
list.clear()
要么
list = new ArrayList<Integer>();
事情发生在随机的时候,我必须清除我的ArrayList中的所有条目,并且我无法知道将来会有多少个新条目,可能有0或1000.哪种方法更快更好,为什么?
This question already has an answer here:
Which one of the 2 options is better and faster to clear an ArrayList, and why?
list.clear()
or
list = new ArrayList<Integer>();
It happens that I have to, at random times, clear all entries from my ArrayList and I have no way to know how many new entries there will be in the future, there might be 0 or a 1000. Which method is faster and better, and why?
原文:https://stackoverflow.com/questions/6961356
最满意答案
逐点:
- 快速解析:二进制(因为你不一定要“解析”,你可以反序列化)
- 添加新事物:文字
- 较小:文本(即使gzip文本大于二进制文件,也不会太大)。
- 可读性:文本
所以这是三个文本的投票,二进制的一个点。 就个人而言,除了图片(以及其他“自然”二进制数据)以外,我都会使用文本。 然后,将所有内容存储在一个大的zip文件中(我可以想到几个游戏做这个或者接近它的东西)。
Point by point:
- Fast Parsing: binary (since you don't necessarily have to "parse", you can just deserialize)
- Adding New Things: text
- Smaller: text (even if gzipped text is larger than binary, it won't be much larger).
- Readability: text
So that's three votes for text, one point for binary. Personally, I'd go with text for everything except images (and other data which is "naturally" binary). Then, store everything in a big zip file (I can think of several games do this or something close to it).
Good reads: The Importance of Being Textual and Power Of Plain Text.
相关问答
更多-
我使用boost.serialization来存储表示查找表的矩阵和向量,以及一些内存大小约为200MB的元数据(字符串)。 IIRC用于从磁盘加载到内存中,文本存档需要3分钟,而使用WinXP上的二进制存档需要4秒。 I used boost.serialization to store matrices and vectors representing lookup tables and some meta data (strings) with an in memory size of about 2 ...
-
您可以使用此问题的答案中提到的选项SORT_PROPERTIES_ALPHABETICALLY来实现稳定的序列化(以及您已经提到的选项ORDER_MAP_ENTRIES_BY_KEYS )。 这些电子邮件中解释了更多背景知识: “......在没有显式声明(通过@JsonPropertyOrder)和一般机制(按字母顺序排序; Object Id和Type Id在其他属性之前)之前,没有定义的排序。” 和 “有一点需要注意的是,Oracle确实改变了JDK 7的行为,因此以前对Introspection返回 ...
-
二进制解析器还是序列化?(Binary parser or serialization?)[2023-09-05]
逐点: 快速解析:二进制(因为你不一定要“解析”,你可以反序列化) 添加新事物:文字 较小:文本(即使gzip文本大于二进制文件,也不会太大)。 可读性:文本 所以这是三个文本的投票,二进制的一个点。 就个人而言,除了图片(以及其他“自然”二进制数据)以外,我都会使用文本。 然后,将所有内容存储在一个大的zip文件中(我可以想到几个游戏做这个或者接近它的东西)。 好读: 纯文本的原文和力量 的重要性 。 Point by point: Fast Parsing: binary (since you don' ... -
1)使用Assembly assembly = Assembly.LoadFrom("dllPath"); 而不是Assembly.LoadFile() 2)由于您的插件具有不同的名称,您可以将它们放在一个目录中,并在app.config中为此目录定义路径。 3)在主AppDomain以外加载Dll可能是个好主意,这样你就可以卸载它们而无需重新启动应用程序,请参阅我的示例 1) Use Assembly assembly = Assembly.LoadFrom("dllPath"); instead of ...
-
好吧,显然有两个独立的东西:DOM作为文档对象模型,DOM作为DOM解析器。 我只是希望他们的名字不同。 ;-) OK, apparently there are 2 separate thing: DOM as a Document Object Model and DOM as a DOM parser. I just wish they would be named differently. ;-)
-
formatter功能运行时没有momentjs上下文。 我猜是因为它消耗formatter属性的方式。 在这里找到的代码中,代码是: if (this.code.formatter) { ... (ctx, varName, this.options.formatter) 由于this关键字有趣的方式,它绑定到对象( options ),并且由于该声明不包含momentjs ,它表示它没有被定义。 通过查看第735行,您可以更好地了解这一点: ctx.pushCode("{0} = ({1}). ...
-
提升二进制序列化问题(Boost Binary Serialization Problem)[2022-07-06]
那么,有一件事.data()没有终端\ 0。 这不是一个C字符串。 我甚至没有意识到stringstream有一个char *构造函数(谁在他们正确的心态使用它们了?),但显然它确实,我敢打赌它期望\ 0。 无论如何,你为什么试图这样做? 用C ++字符串工作会更好。 初始化是用os.str()。 编辑:二进制数据包含大量的\ 0字符和std :: string(char *)构造函数停在第一个。 您的反序列化例程将不可避免地尝试读取流尾(因为它不完整)。 将buf传递到stringstream时,使用st ... -
在您可以反序列化或读取流之前,您必须先回到流的开头示例:ms.Seek(0,SeekOrigin.Begin); static void Main(string[] args) { testclass test = new testclass(); IFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(new byte[512],0,512,true,true ...
-
谷歌协议缓冲区和MessagePack是最有效的方案,不幸的是目前Delphi的端口很少。 Google Protocol Buffers http://sourceforge.net/projects/protobuf-delphi/ 如果你愿意为自己实现(messagepack非常简单),我建议你破解像SuperObject这样的现有库的write()和parse()函数。 你最终会得到一个非常体面的图书馆而不需要太多努力。 Google Protocol Buffers and MessagePack ...
-
Python字符串可以包含二进制数据,因此SerializeAsString返回二进制数据。 Python strings can hold binary data, therefore SerializeAsString returns binary data.