为什么ZeroFormatter很快?

引子

从哪些地方着手提高Serialize速度呢?
这个问题是真正认识“为什么快”的出发点。

先自问自答几个简单的问题:
1,如何更快的实现byte array to hex string?使用一个转换表是一个方法。see Fast byte array to hex string conversion in C#;
2,C#能否支持二进制内存直接访问?可以,使用fixed语句+unsafe方式。

unsafe + fixed 技术

参考fixed语句固定大小的缓冲区

<pre>
unsafe static void TestMethod()
{

// Assume that the following class exists.
//class Point 
//{ 
//    public int x;
//    public int y; 
//}

// Variable pt is a managed variable, subject to garbage collection.
Point pt = new Point();

// Using fixed allows the address of pt members to be taken,
// and "pins" pt so that it is not relocated.

fixed (int* p = &pt.x)
{
    *p = 1;
}        

}
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容