当前位置: 首页 > 编程日记 > 正文

三种序列化方式性能比较

一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间,可是代码显得十分冗余,是否有大神可以指点一二,万分感谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Diagnostics;namespace ProtoBuf
{class Program{static void Main(string[] args){List<Person> list = new List<Person>();int count = 100;double SumTs1 = 0, SumTs2 = 0,SumTs3=0,SumTs4=0,SumTs5=0,SumTs6=0;for (var i=0;i<1000;i++){var person = new Person{Sno = i,Name = "Name" + i,Age = 20 + i,HomeTown="HomeTown"+i,Shool="Shool"+i,Country="Country"+i,Language="Language"+i,Professional="professional"+i,Study="study"+i,FatherName="fatherName"+i,MotherName="motherName"+i};list.Add(person);}do{//binary序列化开始DateTime binaryTime1 = System.DateTime.Now;BinaryFormatter bf = new BinaryFormatter();Stream BinaryStream = new FileStream("e:/person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);//Stream BinaryStream = new MemoryStream();bf.Serialize(BinaryStream, list);BinaryStream.Close();DateTime binaryTime2 = System.DateTime.Now;//binary反序列化开始DateTime debinaryTime1 = System.DateTime.Now;Stream deBinaryStream = new FileStream("e:/person.txt", FileMode.Open, FileAccess.Read, FileShare.Read);//Stream deBinaryStream = new MemoryStream();object obj =bf.Deserialize(BinaryStream);deBinaryStream.Close();DateTime debinaryTime2 = System.DateTime.Now;//Xml序列化开始DateTime XmlTime1 = System.DateTime.Now;XmlSerializer xs = new XmlSerializer(typeof(List<Person>));Stream XmlStream = new FileStream("e:/2person.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);xs.Serialize(XmlStream, list);XmlStream.Close();DateTime XmlTime2 = System.DateTime.Now;//Xml反序列化开始DateTime deXmlTime1 = System.DateTime.Now;XmlSerializer dexs = new XmlSerializer(typeof(List<Person>));Stream deXmlStream = new FileStream("e:/2person.txt", FileMode.Open, FileAccess.Read, FileShare.Read);//Stream deXmlStream = new MemoryStream();List<Person> list2 = (List<Person>)dexs.Deserialize(deXmlStream);deXmlStream.Close();DateTime deXmlTime2 = System.DateTime.Now;//ProtoBuf序列化开始DateTime ProtoBufTime1 = System.DateTime.Now;Stream ProtoBufStream = new MemoryStream();ProtoBuf.Serializer.Serialize(ProtoBufStream, list);ProtoBufStream.Close();DateTime ProtoBufTime2 = System.DateTime.Now;//ProtoBuf反序列化开始DateTime deProtoBufTime1 = System.DateTime.Now;Stream deProtoBufStream = new MemoryStream();Person newPerson = ProtoBuf.Serializer.Deserialize<Person>(deProtoBufStream);deProtoBufStream.Close();DateTime deProtoBufTime2 = System.DateTime.Now;//计算时间和运行次数SumTs1 += binaryTime2.Subtract(binaryTime1).TotalMilliseconds;SumTs2 += debinaryTime2.Subtract(debinaryTime1).TotalMilliseconds;SumTs3 += XmlTime2.Subtract(XmlTime1).TotalMilliseconds;SumTs4 += deXmlTime2.Subtract(deXmlTime1).TotalMilliseconds;SumTs5 += ProtoBufTime2.Subtract(ProtoBufTime1).TotalMilliseconds;SumTs6 += deProtoBufTime2.Subtract(deProtoBufTime1).TotalMilliseconds;count--;} while (count > 0);Console.WriteLine("*********************************序列化比较结果************************************");Console.WriteLine("***            binary序列化        Xml序列化         ProtoBuf序列化        ");Console.WriteLine("***序列化      "+SumTs1/100+"           "+SumTs3/100+"          "+SumTs5/100);Console.WriteLine("***反序列化    "+SumTs2/100+"             "+SumTs4/100+"         "+SumTs6/100);Console.WriteLine("**********************************************************************************");}}[ProtoBuf.ProtoContract][Serializable]public class Person{public int Sno { get; set; }public string Name { get; set; }public int Age { get; set; }public string HomeTown { get; set; }public string Shool { get; set; }public string Country{get;set;}public string Language{get;set;}public string Professional{get;set;}public string Study{get;set;}public string FatherName{get;set;}public string MotherName{get;set;}}   
}

转载于:https://www.cnblogs.com/Freedom0619/p/4121630.html

相关文章:

mac mini 装UBUNTU后没有WIFI解决办法

1、在终端中运行如下命令&#xff0c;重新安装b43相关的全部驱动和firmware: 复制代码代码如下:sudo apt-get install bcmwl-kernel-source #Broadcom 802.11 Linux STA 无线驱动源sudo apt-get install broadcom-sta-commonsudo apt-get install broadcom-sta-sourcesudo apt-…

区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序by Sean Han通过肖恩韩 区块链如何真正起作用&#xff1f; 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.) According to Wikipedia, a blockchain is:根据维基百科&#xff0c;一个区块链是&#xff1…

HDU 4913 Least common multiple

/* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid4913 离散化 线段树 统计逆序数思想 tips: 1、线段树中一定要到处都取模&#xff0c;否则wa。。。 2、lazy是乘积的形式出现&#xff0c;不是加和*/ #include <cstdio> #include <algori…

JS ES6 实用笔记

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 这篇博文我会一直更新。 一.导出导入的两中方式 1.export //demo1.js export const a 6 导入语法为&#xff1a; import {a} form demo1 2.export default //demo2.js export default const b 6 …

extjs editgrid增加一行

Ext.onReady(function(){ /* * EditorGridPanel的工作过程 * 1、用户点击单元格 * 2、单元格按照预设的组件显示单元格的内容并处于编辑状态 * 3、离开单元格的编辑状态 * 4、更新编辑后的内容&#xff0c;出现三角号表示已经被修改过 * 5、程序内部变化&#xff1a;将记录设置…

unity 骨骼击碎_保证击碎$ 100挑战的创新策略

unity 骨骼击碎by Glenn Gonda由Glenn Gonda 保证击碎$ 100挑战的创新策略 (A Creative Strategy Guaranteed to Crush the $100 Challenge) Before I became a software engineer, I made my living as a recording studio engineer. I have a non-traditional background an…

mac下安装libpng环境

用go写一个爬虫工具时需要使用一个go的库&#xff0c;而这个库有需要使用libpng库&#xff0c;不然编译就会提示说 png.h找不到等之类的信息&#xff0c;于是想到应该和windows一样需要安装gcc环境&#xff0c;然后让gcc里安装libpng这个库&#xff0c; 解决办法&#xff1a; 终…

linux oracle修改编码utf8

$ sqlplus /nolog SQL> connect sys/oracle as sysdba SQL> startup 如何设置ORACLE数据库的编码&#xff08;ZHS16GBK&#xff09;修改成UTF8 SQL> shutdown immediate; SQL> startup mount; SQL> alter system enable restricted session; SQL> alter sy…

Vue.js 数据绑定渲染Demo

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 <div id"app">{{ message }} </div>var app new Vue({el: #app,data: {message: Hello Vue!} })Hello Vue!

angular搭建项目步骤_建立健康的Angular项目应采取的步骤

angular搭建项目步骤by Ashish Gaikwad通过Ashish Gaikwad 建立健康的Angular项目应采取的步骤 (Steps you should take to build a healthy Angular project) 使用Jenkins SonarQube创建您的“ Angular Fitbit” (Create your “Angular Fitbit” with Jenkins SonarQube) …

数据库的三大范式和事物

来源&#xff1a;http://blog.csdn.net/w__yi/article/details/19934319 1.1 第一范式&#xff08;1NF&#xff09;无重复的列 1.2 第二范式&#xff08;2NF&#xff09;属性完全依赖于主键 [ 消除部分子函数依赖 ] 1.3 第三范式&#xff08;3NF&#xff09;属性不依赖于其它非…

过滤器和包装器

作者&#xff1a;禅楼望月 过滤器要做的事情 请求过滤器 完成安全检查 重新格式化请求首部或体 建立请求审计或日志响应过滤器 压缩响应流 追加或修改响应流 创建一个完全不同的响应注意不能把过滤器的顺序依赖性硬编码进程序中&#xff0c;它应该由DD控制。 过滤器很像Servlet…

Missing space before value for key 'path'vue.js解决空格报错

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 找到 webpack.base.config.js文件注释掉下面的东西&#xff01;&#xff01; module: { rules: [ /*{ test: /\.(js|vue)$/, loader: eslint-loader, enforce: "p…

现代hy-9600音响_从音响工程师到软件工程师-为什么我要学习编码

现代hy-9600音响by Kalalau Cantrell通过Kalalau Cantrell 从音响工程师到软件工程师-为什么我要学习编码 (From Sound Engineer to Software Engineer — Why I’m Learning to Code) I seriously started teaching myself to code several months ago. I say “seriously” …

微信服务号、公众号、企业号注册

转载于:https://www.cnblogs.com/zhoulaoshi/p/6536850.html

a标签onclick事件解析

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 简单介绍<a>标签的常用点击事件的写法及作用 a href"javascript:void(0);" οnclick"js_method()" //javascript:void(0);作用是返回undefined&#xff0c;地址不发生跳转&am…

安卓版文字扫描识别软件

安卓版文字扫描识别软件 文字识别软件被越来越多的人使用&#xff0c;在使用的过程中也发现了一些问题。总结这些问题发现&#xff0c;很多人对软件能够批量识别这个问题比较关注。如果实现批量识别就可以节省时间。但是一些软件还不能实现批量识别&#xff0c;还有的软件能够做…

中级前端笔试_在短短8个月内如何获得中级前端开发人员的角色

中级前端笔试by Matthew Burfield通过马修伯菲尔德(Matthew Burfield) 在短短8个月内如何获得中级前端开发人员的角色 (How I got a mid-level front end developer role in just 8 months) Three weeks ago I landed a mid-level front-end developer role at a startup. Our…

用stm32f10x建立新的工程重要步骤

stm32f10x系列新建空的工程主要原理&#xff1a; 1.添加启动文件 不同的芯片类型的启动文件的容量是不同的&#xff0c;选择适合该芯片的容量作为启动文件。 注意&#xff1a;启动文件是汇编语言编写的&#xff0c;所以文件的后缀名为.s 2.添加时钟配置 配置文件 stm32f10x.的系…

随机生成6位图片验证码

1. [代码][C#]代码 /// <summary> /// PicHandler1 的摘要说明 /// </summary> public class PicHandler1 : IHttpHandler, IRequiresSessionState { private string mCheckNo string.Empty; protected ImgBuilder _ImgBuilder new I…

html 页面传值

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 直接上代码&#xff0c;JS保存全局变量的三种方式。 创建一个新的JS文件&#xff0c; //quanju.js window.localStorage.JQa"JQA"; window.localStorage.setItem(JQb,JQB);//利用localStora…

node.js的开发流程_Node.js子流程:您需要了解的一切

node.js的开发流程by Samer Buna通过Samer Buna Node.js子流程&#xff1a;您需要了解的一切 (Node.js Child Processes: Everything you need to know) 如何使用spawn()&#xff0c;exec()&#xff0c;execFile()和fork() (How to use spawn(), exec(), execFile(), and fork…

对象存在性检测集中管理

在中大型业务系统中&#xff0c; 常常需要从数据库中查询某个实体对象。 在进行处理之前&#xff0c; 必须先检测该实体是否存在&#xff0c;以增强系统的健壮性。 不过&#xff0c; 检测代码充斥在主业务流程中又会大大降低业务逻辑的清晰性&#xff0c; 最好集中起来进行管理…

20155204 2016-2017-2 《Java程序设计》第3周学习总结

20155204 2016-2017-2 《Java程序设计》第3周学习总结 教材学习内容总结 一个原始码中可以有多个类定义&#xff0c;但只能有一个公开类。留心Scanner对于每一种类型的nextxxxx()方法以Java开头的都是API提供的类使用Integer.valueOf()也是为基本类型建立打包器的方式之一Integ…

js表单提交,支持图片上传,包含后端php代码

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 <html><head><meta http-equiv"Content-Type" content"charsetutf-8" /><title>图片上传成功</title></head><body><form name"…

如何破解汽车-快速的速成课程

by Kenny Kuchera肯尼库切拉(Kenny Kuchera) 如何破解汽车-快速的速成课程 (How to hack a car — a quick crash-course) The goal of this article is to get you started hacking cars — fast, cheap, and easy. In order to do this, we’ll spoof the RPM gauge as an e…

367. Valid Perfect Square

题目&#xff1a; Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: TrueExample 2: Input: 14 Returns: False 链接…

使用reuseport和recvmmsg优化UDP服务器

http://skoo.me/system/2014/03/18/udp-server-performance/ http://www.helplib.net/s/linux.die/65_3223/man-2-recvmmsg.shtml recvmmsg(2) - Linux man page转载于:https://www.cnblogs.com/jingzhishen/p/4145732.html

js把for循环出来的数据存入数组

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 var obj [];for(var i 0;i<obj.length;i){arr.push(obj[i]);}; console.obj(arr); 1&#xff1a;obj是一个数组对象 2&#xff1a;push()方法是数组的栈底添加 意思是往数组的底部添加 3&#xff1a…

flexbox布局_这是您可以使用FlexBox制作的5种布局

flexbox布局The CSS Flexible Box Layout — Flexbox — provides a simple solution to the design and layout problems designers and developers have faced with CSS. Let me show you how to use it to generate some common layouts and challenges that you will face …