Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:43:58+00:00 2026-06-15T10:43:58+00:00

[Update#1] : I’ve uploaded my modified and fixed demo project to https://github.com/sidshetye/SerializersCompare should anyone

  • 0

[Update#1]: I’ve uploaded my modified and fixed “demo” project to https://github.com/sidshetye/SerializersCompare should anyone else be interested in checking out the benchmark.

[Update#2]:I’m seeing that ProtoBufs takes the order of magnitude lead only on subsequent iterations. For a one-time serialization, BinaryFormatter is the one which is an order of magnitude faster. Why? Separate question …

I’m trying to compare BinaryFormatter, Json.NET and ProtoBuf.NET (got the latter off NuGet today). I’m finding that ProtoBuf outputs no real fields, all nulls and 0s (see below). Plus BinaryFormatter appears to be FAR faster. I basically serialized => deserialized the object and compared

  • the original with the regenerated object
  • size in bytes
  • time in ms

Question

  1. How can I get ProtoBuf to actually spit out the real values and not just (default?) values?
  2. What am I doing wrong for speed? I though ProtoBuf was supposed to be the fastest serializer?

The output I got from my test app is below:

Json: Objects identical
Json in UTF-8: 180 bytes, 249.7054 ms

BinaryFormatter: Objects identical
BinaryFormatter: 512 bytes, 1.7864 ms

ProtoBuf: Original and regenerated objects differ !!
====Regenerated Object====
{
    "functionCall": null,
    "parameters": null,
    "name": null,
    "employeeId": 0,
    "raiseRate": 0.0,
    "addressLine1": null,
    "addressLine2": null
}
ProtoBuf: 256 bytes, 117.969 ms

My test was using a simple entity (see below) inside a console application. System: Windows 8×64, VS2012 Update 1, .NET4.5. By the way, I get the same result using the [ProtoContract] and [ProtoMember(X)] convention. Documentation isn’t clear but it appears that DataContract is the newer ‘uniformly’ support convention (right?)

[Serializable]
[DataContract]
class SimpleEntity
{
    [DataMember(Order = 1)]
    public string functionCall {get;set;}

    [DataMember(Order = 2)]
    public string parameters { get; set; }

    [DataMember(Order = 3)]
    public string name { get; set; }

    [DataMember(Order = 4)]
    public int employeeId { get; set; }

    [DataMember(Order = 5)]
    public float raiseRate { get; set; }

    [DataMember(Order = 6)]
    public string addressLine1 { get; set; }

    [DataMember(Order = 7)]
    public string addressLine2 { get; set; }

    public SimpleEntity()
    {
    }

    public void FillDummyData()
    {
        functionCall = "FunctionNameHere";
        parameters = "x=1,y=2,z=3";

        name = "Mickey Mouse";
        employeeId = 1;
        raiseRate = 1.2F;
        addressLine1 = "1 Disney Street";
        addressLine2 = "Disneyland, CA";
    }
}

For those interested here is the snippet of my AllSerializers class for ProtoBufs

public byte[] SerProtoBuf(object thisObj)
{
    using (MemoryStream ms = new MemoryStream())
    {
        Serializer.Serialize(ms, thisObj);
        return ms.GetBuffer();
    }
}

public T DeserProtoBuf<T>(byte[] bytes)
{

    using (MemoryStream ms = new MemoryStream())
    {
        ms.Read(bytes, 0, bytes.Count());
        return Serializer.Deserialize<T>(ms);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:43:59+00:00Added an answer on June 15, 2026 at 10:43 am

    Firstly, your serialize / deserialize methods are both broken; you are over-reporting the result (GetBuffer(), without Length), and you aren’t writing anything into the stream for deserialization. Here’s a correct implementation (although you could also use GetBuffer() if you were returning ArraySegment<byte>):

    public static byte[] SerProtoBuf(object thisObj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.NonGeneric.Serialize(ms, thisObj);
            return ms.ToArray();
        }
    }
    
    public static T DeserProtoBuf<T>(byte[] bytes)
    {
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return Serializer.Deserialize<T>(ms);
        }
    }
    

    That is why you are getting no data back. Secondly, you don’t say how you are timing it, so here’s some I’ve written based on your code (which also includes code to show that it is getting all the values back). Results first:

    Via BinaryFormatter:
    1 Disney Street
    Disneyland, CA
    1
    FunctionNameHere
    Mickey Mouse
    x=1,y=2,z=3
    1.2
    
    Via protobuf-net:
    1 Disney Street
    Disneyland, CA
    1
    FunctionNameHere
    Mickey Mouse
    x=1,y=2,z=3
    1.2
    
    Serialize BinaryFormatter: 112 ms, 434 bytes
    Deserialize BinaryFormatter: 113 ms
    Serialize protobuf-net: 14 ms, 85 bytes
    Deserialize protobuf-net: 19 ms
    

    Analysis:

    Both serializers stored the same data; protobuf-net was an order of magnitude faster, and a factor of 5 smaller output. I declare: winner.

    Code:

    static BinaryFormatter bf = new BinaryFormatter();
    public static byte[] SerBinaryFormatter(object thisObj)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, thisObj);
            return ms.ToArray();
        }
    }
    
    public static T DeserBinaryFormatter<T>(byte[] bytes)
    {
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return (T)bf.Deserialize(ms);
        }
    }
    static void Main()
    {
        SimpleEntity obj = new SimpleEntity(), clone;
        obj.FillDummyData();
    
        // test that we get non-zero bytes
        var data = SerBinaryFormatter(obj);
        clone = DeserBinaryFormatter<SimpleEntity>(data);
        Console.WriteLine("Via BinaryFormatter:");
        Console.WriteLine(clone.addressLine1);
        Console.WriteLine(clone.addressLine2);
        Console.WriteLine(clone.employeeId);
        Console.WriteLine(clone.functionCall);
        Console.WriteLine(clone.name);
        Console.WriteLine(clone.parameters);
        Console.WriteLine(clone.raiseRate);
        Console.WriteLine();
    
        data = SerProtoBuf(obj);
        clone = DeserProtoBuf<SimpleEntity>(data);
        Console.WriteLine("Via protobuf-net:");
        Console.WriteLine(clone.addressLine1);
        Console.WriteLine(clone.addressLine2);
        Console.WriteLine(clone.employeeId);
        Console.WriteLine(clone.functionCall);
        Console.WriteLine(clone.name);
        Console.WriteLine(clone.parameters);
        Console.WriteLine(clone.raiseRate);
        Console.WriteLine();
    
        Stopwatch watch = new Stopwatch();
        const int LOOP = 10000;
    
        watch.Reset();
        watch.Start();
        for (int i = 0; i < LOOP; i++)
        {
            data = SerBinaryFormatter(obj);
        }
        watch.Stop();
        Console.WriteLine("Serialize BinaryFormatter: {0} ms, {1} bytes", watch.ElapsedMilliseconds, data.Length);
    
        watch.Reset();
        watch.Start();
        for (int i = 0; i < LOOP; i++)
        {
            clone = DeserBinaryFormatter<SimpleEntity>(data);
        }
        watch.Stop();
        Console.WriteLine("Deserialize BinaryFormatter: {0} ms", watch.ElapsedMilliseconds, data.Length);
    
        watch.Reset();
        watch.Start();
        for (int i = 0; i < LOOP; i++)
        {
            data = SerProtoBuf(obj);
        }
        watch.Stop();
        Console.WriteLine("Serialize protobuf-net: {0} ms, {1} bytes", watch.ElapsedMilliseconds, data.Length);
    
        watch.Reset();
        watch.Start();
        for (int i = 0; i < LOOP; i++)
        {
            clone = DeserProtoBuf<SimpleEntity>(data);
        }
        watch.Stop();
        Console.WriteLine("Deserialize protobuf-net: {0} ms", watch.ElapsedMilliseconds, data.Length);
    }
    

    Lastly, [DataMember(...)] support isn’t really the “newer ‘uniformly’ support convention” – it certainly isn’t “newer” – I’m pretty sure it has supported both of those since something like commit #4 (and possibly earlier). It is just options provided for convenience:

    • not all target platforms have DataMemberAttribute
    • some people prefer to limit the DTO layer to inbuilt markers
    • some types are largely outside your control, but may already have those markers (generated data from LINQ-to-SQL, for example)
    • additionally, note that 2.x allows you to define the model at runtime without having to add attributes (although attributes remain the most convenient way to do it)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update 2018 TL;DR; LaTEX for WPF https://github.com/ForNeVeR/wpf-math Original question I need to have a
UPDATE: Please see https://softwarerecs.stackexchange.com/questions/71464/java-library-to-insert-invisible-text-into-a-pdf instead. I want to insert invisible text into an existing
Update: I reported this as a bug to Apple and they fixed it! All
Update: Switching to IIS Express instead of the Visual Studio Development Server fixed the
UPDATE: I'm getting this error: (No route matches /docs/index.html... ) when accessing admin.example.com/docs/index.html The
UPDATE 10 Secs later Fixed properly now, and thanks to JF and Gauden. UPDATE
[update] My bad.. I didn't look through the codes properly.. I should have spotted
UPDATE: I found a Scipy Recipe based in this question! So, for anyone interested,
Update: Is there a way to achieve what I'm trying to do in an
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.