- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个长度为 2.2 亿(固定)的 int 和 float 数组。现在,我想将这些数组存储/上传到/从内存和磁盘。目前,我正在使用 Java NIO 的 FileChannel 和 MappedByteBuffer 来解决这个问题。它工作正常,但将阵列存储到/从内存到磁盘需要大约 5 秒(挂钟时间)。现在,我想让它更快。
在这里,我应该提到大多数数组元素都是 0(将近 52%)。
喜欢:
int arr1 [] = { 0 , 0 , 6 , 7 , 1, 0 , 0 ...}
任何人都可以帮助我,有什么好的方法可以通过不存储或加载那些 0 来提高速度。这可以通过使用 Arrays.fill (array , 0) 来补偿。
最佳答案
以下方法需要磁盘上的 n/8 + nz * 4 个字节,其中 n 是数组的大小,nz 是非零条目的数量。对于 52% 的零条目,您可以将存储大小减少 52% - 3% = 49%。
你可以这样做:
void write(int[] array) {
BitSet zeroes = new BitSet();
for (int i = 0; i < array.length; i++)
zeroes.set(i, array[i] == 0);
write(zeroes); // one bit per index
for (int i = 0; i < array.length; i++)
if (array[i] != 0)
write(array[y]);
}
int[] read() {
BitSet zeroes = readBitSet();
array = new int[zeroes.length];
for (int i = 0; i < zeroes.length; i++) {
if (zeroes.get(i)) {
// nothing to do (array[i] was initialized to 0)
} else {
array[i] = readInt();
}
}
}
编辑:你说这稍微慢一点意味着磁盘不是瓶颈。您可以通过在构造时写入位集来调整上述方法,这样您就不必在将位集写入磁盘之前将其写入内存。此外,通过逐字写入 bitset 并穿插实际数据,我们可以只对数组进行一次传递,从而减少缓存未命中:
void write(int[] array) {
writeInt(array.length);
int ni;
for (int i = 0; i < array.length; i = ni) {
ni = i + 32;
int zeroesMap = 0;
for (j = i + 31; j >= i; j--) {
zeroesMap <<= 1;
if (array[j] == 0) {
zeroesMap |= 1;
}
}
writeInt(zeroesMap);
for (j = i; j < ni; j++)
if (array[j] != 0) {
writeInt(array[j]);
}
}
}
}
int[] read() {
int[] array = new int[readInt()];
int ni;
for (int i = 0; i < array.length; i = ni) {
ni = i + 32;
zeroesMap = readInt();
for (j = i; j < ni; j++) {
if (zeroesMap & 1 == 1) {
// nothing to do (array[i] was initialized to 0)
} else {
array[j] = readInt();
}
zeroesMap >>= 1;
}
}
return array;
}
(前面的代码假定 array.length 是 32 的倍数。如果不是,则按照您喜欢的方式写入数组的最后一个切片)
如果这也不能减少处理时间,则压缩不是可行的方法(我认为任何通用压缩算法都不会比上述算法更快)。
关于java - 写 "compressed"数组提高IO性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249718/
zlib中的deflate()和compress()函数之间有什么区别? 我浏览了在线示例,其中一些使用deflate,而另一些使用compress。 我应该如何决定在哪种情况下使用另一种情况? 最佳
这是我的config.rb preferred_syntax = :sass http_path = '/' css_dir = 'css' sass_dir = 'sass' images_dir
我正在查看 Oracle 的 OLTP 表压缩(11g 及以上)文档以及在线资源以查找语法并遇到两个不同的版本: COMPRESS FOR ALL OPERATIONS 和 COMPRESS FOR
我目前在 .NET 2.0 下使用 SharpZipLib,通过它我需要将单个文件压缩为单个压缩存档。为此,我目前正在使用以下内容: string tempFilePath = @"C:\Users\
本文整理了Java中org.apache.hadoop.io.compress.zlib.ZlibCompressor.compress()方法的一些代码示例,展示了ZlibCompressor.co
我从 Azure Synapse 收到此建议。 推荐详情我们检测到您可以通过拆分存储帐户中暂存的压缩文件来提高负载吞吐量。一个好的经验法则是将压缩文件拆分为 60 个或更多,以最大限度地提高负载的并行
这个问题继续讨论开始 here 。我发现 HTTP 响应正文无法被解码为 JSON 对象,因为后者的 deflate 压缩。现在我想知道如何用Golang进行解压。我将不胜感激任何能够显示我的代码中的
我刚开始为新雇主工作。对于我的上一个雇主,我们使用 LESS 并将其编译为 .css 文件,然后将其压缩为 .min.css 文件。对于我的新雇主,我们也使用 LESS。但是这里我们不仅编译了LESS
我在终端中使用 compress file.txt 压缩了一个文件并得到了(如预期的那样)file.txt.Z 当我将该文件传递给 Go 中的 ioutil.ReadFile 时, buf0, err
当我打开Android Studio时,Android Studio将从commons-compress-1.12.jar下载https://jcenter.bintray.com/org/apach
我收到此错误,但我不知道为什么突然遇到 django-pipeline 问题。 我正在 virtualenv 下运行该项目,我尝试使用所需的基本应用程序创建一个新项目,看看它是否可以修复它,但我总是收
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
对于压缩由 fasta 核苷酸序列组成的数据集,推荐的压缩算法是什么(.xz、tar.gz、tar.bz2 等)? 此类数据的推荐压缩机制是什么? 基于字典的压缩 基于自适应字典的压缩 基于LZW算法
我正在寻找一个支持解压期间随机访问的通用压缩库。我想将维基百科压缩成单一的压缩格式,同时我想从中解压/提取个别文章。 当然,我可以单独压缩每篇文章,但这不会给出太大的压缩率。我听说 LZO 压缩文件由
我想压缩一串位,然后我想解压缩它。任何机构都可以通过提及快速无损压缩和解压缩技术来帮助我,如果可能的话,它的编程实现可以帮助我。 最佳答案 如果您正在寻找速度,那么考虑像 LZ4 这样的快速压缩算法是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 4年前关闭。 Improve this qu
当我们压缩一个文件夹时,我们输入命令tar -cjf folder.tar.bz2 folder,它会将整个文件夹压缩到其中。 无论如何要压缩文件夹中的所有内容,但该文件夹不应出现在存档中? 示例 -
来自 Shannon's Source Coding Theorem我们知道压缩字符串的熵受原始字符串熵的限制,如下所示: H(X) <= L < H(X) + 1/N 其中 H(X) 是源字符串的熵
假设您有一个四字节整数,并且您想将其压缩为更少的字节。您可以压缩它,因为较小的值比较大的值更有可能(即,值的概率随其大小而降低)。您应用以下方案来生成 1、2、3 或 4 字节的结果: 请注意,在下面
是否有可能使用某种简单算法打包大文件,使我读取数据的速度比未压缩文件快(由于硬盘驱动器比未压缩速度慢)? 我需要什么样的压缩率?任何快速压缩算法都能做到吗? 最佳答案 是。当从硬盘上读取压缩系数为4的
我是一名优秀的程序员,十分优秀!