English 中文(简体)
Most performant way to write binary data to a file in C#
原标题:

I am trying to optimize a class that serializes objects in binary format and writes them in a file. I am currently using a FileStream (in sync mode because of the size of my objects) and a BinaryWriter. Here s what my class looks like:

public class MyClass
{
    private readonly BinaryWriter m_binaryWriter;
    private readonly Stream m_stream;

    public MyClass()
    {
        // Leave FileStream in synchronous mode for performance issue (faster in sync mode)
        FileStream fileStream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite, maxSize, false);
        m_stream = fileStream;

        m_binaryWriter = new BinaryWriter(m_stream);
    }

    public void SerializeObject(IMySerializableObject serializableObject)
    {
        serializableObject.Serialize(m_binaryWriter);
        m_stream.Flush();
    }
}

A profiler run on this code shows good performance but I was wondering if there are other objects (or techniques) that I could use to improve the performance of this class.

最佳回答

Yes - you could use a different serialization format. The built-in serialization format is rich, but has downsides too - it s quite verbose compared with some other custom formats.

The format I m most familiar with is Protocol Buffers, which is an efficient and portable binary format from Google. It does, however, require you to design the types that you want to serialize in a different way. There are always pros and cons :)

There are other binary serialization formats too, such as Thrift.

You may want to stick to the built-in serialization, but it s worth knowing that other options are available.

Before you go too far, however, you should determine what you care about and how much you actually need to worry about the performance anyway. You could waste a lot of time investigating options when what you ve got may be fine as it is :)

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签