gpt4 book ai didi

performance - 使用 Serde 和 Bincode 将大型结构序列化到磁盘很慢

转载 作者:行者123 更新时间:2023-11-29 07:50:50 26 4
gpt4 key购买 nike

<分区>

我有一个包含 2³¹ u32 值的向量的结构(总大小约为 8GB)。我按照 bincode 示例将其写入磁盘:

#[macro_use]
extern crate serde_derive;
extern crate bincode;

use std::fs::File;
use bincode::serialize_into;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct MyStruct {
counter: Vec<u32>,
offset: usize,
}

impl MyStruct {
// omitted for conciseness
}


fn main() {
let m = MyStruct::new();

// fill entries in the counter vector

let mut f = File::create("/tmp/foo.bar").unwrap();
serialize_into(&mut f, &m).unwrap();
}

为了避免分配内存两次,我使用serialize_into直接写入文件。不过写的过程真的很慢(大概半个小时)。有什么办法可以加快速度吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com