【问题标题】:Binary Serialization of an Array of DateTimeDateTime 数组的二进制序列化
【发布时间】:2010-10-29 17:31:18
【问题描述】:

当我使用 BinaryFormatter 序列化 Int32 数组时,我得到大约 400MB/s(一秒内 1 亿个项目),但是当我尝试序列化 DateTime 数组时,我只得到大约 27MB/s 的吞吐量(30 秒内 1 亿个项目)。一个 DateTime 以序列化形式占用 8 个字节。我猜 BinaryFormatter 使用 ISerializable 接口,如果它实现了,所以我看了一下 DateTime 类型的 GetObjectData 实现:

void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
    if (info == null)
    {
        throw new ArgumentNullException("info");
    }
    info.AddValue("ticks", this.InternalTicks);
    info.AddValue("dateData", this.dateData);
}

我很困惑将 UInt64 和 Int64 添加到输出中,总和应该是 16 个字节,但这并不反映我的措施。那么 DateTime 是如何真正序列化为二进制的呢?

【问题讨论】:

    标签: c# arrays datetime serialization binary


    【解决方案1】:

    听起来您的 int 序列化受 I/O 限制,但您的 DateTime 序列化受 CPU 限制(显然,序列化 DateTime 比序列化 int 需要更多的 CPU 时间)。因此,您的测量值不会反映您的两种数据类型大小之间的确切比率。

    【讨论】:

    • 测量是通过序列化到 MemoryStream 来完成的,所以没有 I/O。
    • 好的,所以你没有 I/O 限制,但是序列化 DateTime 显然比 int 长 30 倍。
    【解决方案2】:

    它会超过 16 个字节,因为它也需要存储密钥。

    您可以自己(直接)序列化刻度吗?或者;我没有在这个场景下测试过,但是你可以试一试protobuf-net(它是一个高性能的二进制序列化引擎)。

    【讨论】:

    • 1 亿个 DateTimes 给了我大约 800MB 的流长度,所以 BinaryFormatter 似乎存储了一些类似刻度的东西,但不幸的是,速度很慢。
    猜你喜欢
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2014-01-04
    • 2016-09-15
    • 2016-09-29
    • 2013-08-16
    • 2016-07-27
    • 1970-01-01
    相关资源
    最近更新 更多