处理大量整数的最佳方法

发布于 2024-08-20 20:55:03 字数 81 浏览 3 评论 0原文

我有一个大约 10-100k 整数的数组,需要存储(尽可能压缩),并以最快的方式检索回完整数组。使用 c# 这样的语言处理此类事情的最佳方法是什么?

I have an array of about 10-100k ints that I need to store (as compressed as possible), and retrieve back to the complete array the fastest way possible. What is the best way to handle this type of thing in a language like c#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难以启齿的温柔 2024-08-27 20:55:03

这取决于“尽可能压缩”的意思。

您可以使用 BinaryWriter 将整数写入流,或使用 BitConverter.GetBytes 将每个 int 作为四个字节复制到一个大数组中。两者都会存储每个 int 而不需要任何额外的元数据。

如果您希望它比这更压缩,BinaryWriter 有一个 Write7BitEncodedInt 方法,可以用更少的字节写入小值的整数。将数据打包到字节数组后,您还可以使用 GZipStream 类尝试进一步压缩数据。

一般来说,你想要的越小,处理时间就越长。为了获得您想要的速度和大小之间的平衡,您只需进行一些测试。

That depends on what you mean by "as compressed as possible".

You can use a BinaryWriter to write the integers to a stream, or use BitConverter.GetBytes to get each int as four bytes as copy into a large array. Either would store each int without any extra meta data.

If you want it more compressed than that, the BinaryWriter has a Write7BitEncodedInt method that writes ints with small values in fewer bytes. You can also use the GZipStream class to try to further compress the data once you have it packed in a byte array.

Generally, the smaller you want it, the longer it will take to process. To get the balance between speed and size that you want, you just have to do some testing.

猫瑾少女 2024-08-27 20:55:03

根据此 int 数组中值的性质,行程编码可能是另一种选择。也就是说,如果数组中的连续单元格都具有相同的值,则只需存储该序列中该值的第一次出现以及此后重复出现的次数。这对于“稀疏”数据可能特别有效。

Depending on the nature of the values in this int array, run-length encoding might be another option. That is, if contiguous cells in your array all have the same value, you only need to store the first occurrence of the value in that sequence, along with the number of times it will be repeated after that. This might work especially well with "sparse" data.

段念尘 2024-08-27 20:55:03

100,000 个整数并没有那么大,为什么需要压缩这么多呢?

100,000 ints is not that big, why do you need to compress it so much?

不打扰别人 2024-08-27 20:55:03

回答您的具体问题

  1. 选择足够大且仅足以存储您的数据的数据类型,例如 uint32_t 或 int64_t。注意:它必须是固定长度。
  2. 将数据以二进制形式连续写入到文件中。
  3. 将数据直接读回到您的数组类型的内存中。

问题以最优化的方式解决。如果您想要磁盘压缩,请通过压缩库运行数据。当您尝试使用数据时,将数据压缩在内存中通常是不允许的(一般解决方案使用其他技术)。如果您需要了解为什么这是禁忌的信息,请注明。

大型数据集计算的一般答案

专门的数学库处理这些问题(例如,octave 或 matlab),特别是处理超出您计算机所能想象的数量的问题。

这些库具有执行引擎和特定语言,但您通常可以通过编程方式与它们交互。

Answer for your specific question

  1. Pick the data type that is large enough and only large enough to store your data --e.g., uint32_t or int64_t. Note: it has to be fixed length.
  2. Write the data out in binary form -- back to back -- to a file.
  3. Read the data back directly into the memory of your array type.

Problem solved in most optimal way. If you wanted on-disk compression, run the data through a zipping library. having the data compressed in-memory while you are trying to use it is generally a no-no (the general solution uses other techniques). Please indicate if you want information why it is a no-no.

General answer for computing with large data-sets

Specialized mathematics libraries deal with these issues (e.g., octave or matlab), specifically the issues of dealing with more numbers than you can think possible with your computer.

These libraries have an execution engine and a specific language, but you can often programmatically interface with them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文