- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个需要二进制序列化的类。该类包含一个字段,如下所示:
private T[,] m_data;
这些多维数组可以相当大(数十万个元素)并且可以是任何原始类型。当我尝试对对象进行标准 .net 序列化时,写入磁盘的文件很大,我认为 .net 存储了大量关于元素类型的重复数据,可能效率不高。
我四处寻找自定义序列化程序,但没有看到任何处理多维通用数组的序列化程序。在序列化并取得一些成功之后,我还在内存流的字节数组上尝试了内置的 .net 压缩,但没有我希望的那么快/压缩。
我的问题是,我应该尝试编写一个自定义序列化程序来针对适当的类型优化序列化此数组(这似乎有点令人生畏),还是应该使用标准 .net 序列化并添加压缩?
任何有关最佳方法的建议,或显示如何处理多维通用数组序列化的资源链接,我们将不胜感激 - 如前所述 existing examples我发现不支持这种结构。
最佳答案
这是我想出的。下面的代码生成一个 int[1000][10000] 并使用 BinaryFormatter 将其写入 2 个文件 - 一个压缩,一个不压缩。
压缩文件为 1.19 MB(1,255,339 字节)解压后为 38.2 MB(40,150,034 字节)
int width = 1000;
int height = 10000;
List<int[]> list = new List<int[]>();
for (int i = 0; i < height; i++)
{
list.Add(Enumerable.Range(0, width).ToArray());
}
int[][] bazillionInts = list.ToArray();
using (FileStream fsZ = new FileStream("c:\\temp_zipped.txt", FileMode.Create))
using (FileStream fs = new FileStream("c:\\temp_notZipped.txt", FileMode.Create))
using (GZipStream gz = new GZipStream(fsZ, CompressionMode.Compress))
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(gz, bazillionInts);
f.Serialize(fs, bazillionInts);
}
我想不出更好/更简单的方法来做到这一点。压缩版非常紧。
我会选择 BinaryFormatter + GZipStream。定制一些东西一点也不有趣。
[MG编辑]我希望您不会被编辑冒犯,但是统一重复的 Range(0,width) 极大地扭曲了事情;更改为:
int width = 1000;
int height = 10000;
Random rand = new Random(123456);
int[,] bazillionInts = new int[width, height];
for(int i = 0 ; i < width;i++)
for (int j = 0; j < height; j++)
{
bazillionInts[i, j] = rand.Next(50000);
}
尝试一下;您会看到 temp_notZipped.txt
为 40MB,temp_zipped.txt
为 62MB。不太吸引人...
关于c# - 优化多维泛型数组的二进制序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/223700/
我的数据库表是: author book repeat ------ ----
众所周知,我们可以简化 SELECT * FROM A WHERE X=1 OR X=4 OR X=9 如下: SELECT * FROM A WHERE X IN (1,4,9) “多维”条件是否有
我在大学时正在编写 Android 应用程序。 用户被询问必须回答的问题。 我通过服务器的 HTTP 请求/响应获取问题。它们以字符串形式传递。该字符串看起来像这样,有两个问题: (requested
我怎样才能在 C++ 中使用 vector 制作这样的表格: 65 A 66 B 67 C 我是用这样的动态二维数组完成的: int** ary = new int*[2]; for (int
运行时: if (data.custaccount.webaddress) { alert('found it'); } 我得到了错误 data.custaccount is undefined
如何按名称对对象数组进行排序。 数组构造数组的例子: object[] o = new object[items.Count+(row-exists)]; int i = 0;
如何创建二维 vector ?我知道在二维数组中,我可以这样表达: a[0][1]=98; a[0][2]=95; a[0][3]=99; a[0][4]=910; a[1][0]=98; a[1][
这是我的第一个问题。 我有很多组数据。它们中的每一个都应该在 DataFrame 中呈现。我试图通过将 DataFrame 作为多维元组的一项来实现这一点,例如: data[0][1].Glucose
有人可以建议改进我对多维 lstm 的实现吗? 它非常慢并且使用大量内存。 class MultiDimentionalLSTMCell(tf.nn.rnn_cell.RNNCell): """ Ad
我已为我在另一个基于 WPF 的应用程序中使用的 Azure 移动服务添加了到 MVC 站点的连接。 我不太熟悉如何通过移动服务在 MVC 中显示数据,找不到任何很好的示例来展示如何在页面上(例如在网
这样写对吗?有没有更好的写法呢?这与我正在使用的真实数据类似,我想确保将对象或数组正确嵌套在 JSON 文件中。 var data = [ { "department": "I
我有一个HashMap当我调用 .toString() 时,它看起来像这样: {somekey=false, anotherKey=someString, thirdKey={nestedKey=he
是否有更 pythonic 的方式来执行以下操作: import numpy as np def diagonal(A): (x,y,y) = A.shape diags = []
我有以下结构: import java.util.LinkedHashMap; ... LinkedHashMap level0 = new LinkedHashMap(); LinkedHashMa
如何访问 Arraylist 中的整数数组(两者均可调整大小)? 到目前为止我有这个代码: List vertices_passed = new ArrayList(); 我想进入vertices_p
我想知道 MiniZinc 语言中是否可以有(多维)数组的数组。 确实,我想解决 worker 的时间表问题。我的目标是检查它们是否每周至少有 1 天可用。每个 worker 都由一个整数索引,我每周
这个问题已经有答案了: how is axis indexed in numpy's array? (5 个回答) 已关闭 3 年前。 我还没有理解 NumPy 中多维数组中的轴之间的区别。你能给我解
我试图 $.post 包含一些表单数据以及数组中的一些其他数据。我可以将数组插入表单数据的其余部分,但我丢失了数组中的键,并且数据仅表示为字符串,我想我要求的是多维数组? 这是当前的响应字符串: ar
我是 numpy 新手,试图理解 here 中的以下示例。我无法理解的输出 >>> palette[image] 当索引数组 a 是多维时,单个索引数组引用 a 的第一个维度。以下示例通过使用调色板将
这个问题已经有答案了: JavaScript associative array to JSON (5 个回答) JSON.stringify doesn't work with normal Jav
我是一名优秀的程序员,十分优秀!