首页 > 代码库 > 字符串压缩性能

字符串压缩性能

先上源码:

 1 using System;
 2 using System.Diagnostics;
 3 using System.IO;
 4 using System.IO.Compression;
 5 using System.Text;
 6 
 7 namespace TestCompress
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string source = "{\"users\":[{\"id\":1662047260,\"idstr\":\"1662047260\",\"screen_name\":\"SinaAppEngine\",\"name\":\"SinaAppEngine\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"Sina App Engine(简称SAE),简单高效的分布式Web服务开发、运行平台。\\n新浪云平台Sina App Engine官网(...\",\"url\":\"http://sae.sina.com.cn\",\"profile_image_url\":\"http://tp1.sinaimg.cn/1662047260/50/5633919323/1\",\"profile_url\":\"saet\",\"domain\":\"saet\",\"weihao\":\"\",\"gender\":\"m\",\"followers_count\":193403,\"friends_count\":155,\"statuses_count\":4288,\"favourites_count\":16,\"created_at\":\"Thu Nov 19 14:47:16 +0800 2009\",\"following\":false,\"allow_all_act_msg\":true,\"geo_enabled\":true,\"verified\":true,\"verified_type\":2,\"status_id\":3496335523491079,\"allow_all_comment\":true,\"avatar_large\":\"http://tp1.sinaimg.cn/1662047260/180/5633919323/1\",\"verified_reason\":\"Sina App Engine官方微博\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":116,\"lang\":\"zh-cn\"},{\"id\":1936344094,\"idstr\":\"1936344094\",\"screen_name\":\"微游戏平台\",\"name\":\"微游戏平台\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"微游戏平台——\\n新浪微博官方游戏平台\\n@微游戏平台 使命\\n1、成为合作伙伴了解微游戏最快速、最便捷的通道;\\n2、为微游戏合作伙伴的交流沟...\",\"url\":\"http://game.weibo.com\",\"profile_image_url\":\"http://tp3.sinaimg.cn/1936344094/50/5605578586/1\",\"profile_url\":\"pingtaiv\",\"domain\":\"pingtaiv\",\"weihao\":\"\",\"gender\":\"m\",\"followers_count\":2332,\"friends_count\":84,\"statuses_count\":288,\"favourites_count\":0,\"created_at\":\"Tue May 17 17:05:03 +0800 2011\",\"following\":false,\"allow_all_act_msg\":true,\"geo_enabled\":true,\"verified\":true,\"verified_type\":2,\"status_id\":3490521190033344,\"allow_all_comment\":true,\"avatar_large\":\"http://tp3.sinaimg.cn/1936344094/180/5605578586/1\",\"verified_reason\":\"新浪微游戏开放平台官方微博\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":65,\"lang\":\"zh-cn\"},{\"id\":1949305184,\"idstr\":\"1949305184\",\"screen_name\":\"微博桌面\",\"name\":\"微博桌面\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"【微博桌面】是新浪微博官方的微博客户端软件。\\r\\n方便你在电脑上随手发微博、与好友聊话题哦!\\r\\n下载地址:http://desktop.weibo.com/\",\"url\":\"http://desktop.weibo.com\",\"profile_image_url\":\"http://tp1.sinaimg.cn/1949305184/50/5635814569/1\",\"profile_url\":\"weibodesktop\",\"domain\":\"weibodesktop\",\"weihao\":\"\",\"gender\":\"m\",\"followers_count\":810622,\"friends_count\":252,\"statuses_count\":265,\"favourites_count\":158,\"created_at\":\"Fri May 20 13:15:49 +0800 2011\",\"following\":false,\"allow_all_act_msg\":true,\"geo_enabled\":true,\"verified\":true,\"verified_type\":2,\"status_id\":3495731698678415,\"allow_all_comment\":true,\"avatar_large\":\"http://tp1.sinaimg.cn/1949305184/180/5635814569/1\",\"verified_reason\":\"新浪微博桌面客户端官方账号\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":128,\"lang\":\"zh-cn\"},{\"id\":1642909335,\"idstr\":\"1642909335\",\"screen_name\":\"微博小秘书\",\"name\":\"微博小秘书\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"申请认证请看:http://weibo.com/verify;认证疑问请私信给@微博客服;客服电话:4000 960 960(个人) 4000 980 980(企业)\",\"url\":\"http://blog.sina.com.cn/sinat\",\"profile_image_url\":\"http://tp4.sinaimg.cn/1642909335/50/5597136100/0\",\"profile_url\":\"sinat\",\"domain\":\"sinat\",\"weihao\":\"\",\"gender\":\"f\",\"followers_count\":48747791,\"friends_count\":1035,\"statuses_count\":2534,\"favourites_count\":7,\"created_at\":\"Sun Aug 30 00:14:07 +0800 2009\",\"following\":false,\"allow_all_act_msg\":true,\"geo_enabled\":false,\"verified\":true,\"verified_type\":3,\"status_id\":3495628988141995,\"allow_all_comment\":true,\"avatar_large\":\"http://tp4.sinaimg.cn/1642909335/180/5597136100/0\",\"verified_reason\":\"新浪微博官方账号\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":439,\"lang\":\"zh-cn\"},{\"id\":1781387491,\"idstr\":\"1781387491\",\"screen_name\":\"微博iPhone客户端\",\"name\":\"微博iPhone客户端\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"欢迎来到@微博iPhone客户端 官方微博!\\n在这里:\\n轻松更新浏览你关注的好友、娱乐明星、专家发布的最新微博;即时获取国内外热点新闻,...\",\"url\":\"http://news.sina.com.cn/wap/wbclient.html\",\"profile_image_url\":\"http://tp4.sinaimg.cn/1781387491/50/5597471732/0\",\"profile_url\":\"58351\",\"domain\":\"weiboiphone\",\"weihao\":\"58351\",\"gender\":\"f\",\"followers_count\":10949498,\"friends_count\":71,\"statuses_count\":605,\"favourites_count\":128,\"created_at\":\"Thu Jul 22 12:12:30 +0800 2010\",\"following\":false,\"allow_all_act_msg\":true,\"geo_enabled\":true,\"verified\":true,\"verified_type\":2,\"status_id\":3495946085897922,\"allow_all_comment\":true,\"avatar_large\":\"http://tp4.sinaimg.cn/1781387491/180/5597471732/0\",\"verified_reason\":\"新浪微博iPhone客户端客服帐号\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":53,\"lang\":\"zh-cn\"},{\"id\":1904178193,\"idstr\":\"1904178193\",\"screen_name\":\"微博开放平台\",\"name\":\"微博开放平台\",\"province\":\"11\",\"city\":\"8\",\"location\":\"北京 海淀区\",\"description\":\"【平台公告】自2012年10月15日起,微博开放平台将禁止“累计使用人数小于10,000”的应用,使用V1接口。为保证应用正常运行,请开...\",\"url\":\"\",\"profile_image_url\":\"http://tp2.sinaimg.cn/1904178193/50/5610154048/0\",\"profile_url\":\"openapi\",\"domain\":\"openapi\",\"weihao\":\"\",\"gender\":\"f\",\"followers_count\":57991,\"friends_count\":31,\"statuses_count\":1003,\"favourites_count\":2,\"created_at\":\"Mon Dec 27 17:56:46 +0800 2010\",\"following\":false,\"allow_all_act_msg\":false,\"geo_enabled\":true,\"verified\":true,\"verified_type\":2,\"status_id\":3494540847151431,\"allow_all_comment\":false,\"avatar_large\":\"http://tp2.sinaimg.cn/1904178193/180/5610154048/0\",\"verified_reason\":\"新浪微博开放平台\",\"follow_me\":false,\"online_status\":0,\"bi_followers_count\":26,\"lang\":\"zh-cn\"}],\"next_cursor\":0,\"previous_cursor\":0,\"total_number\":6}";
14             NewLifeCore_Compress(source);
15             System_Compress(source);
16             Console.ReadKey();
17         }
18 
19         static void NewLifeCore_Compress(string source)
20         {
21             Stopwatch time = new Stopwatch();
22             time.Start();
23             for (int i = 0; i < 10000; i++)
24             {
25                 byte[] buf = Encoding.UTF8.GetBytes(source);
26                 buf = buf.Compress();
27             }
28             time.Stop();
29             Console.WriteLine("NewLifeCore_Compress\t压缩10000次,耗时:{0}毫秒。", time.ElapsedMilliseconds);
30         }
31 
32         public static void System_Compress(string source)
33         {
34             Stopwatch time = new Stopwatch();
35             time.Start();
36             for (int i = 0; i < 10000; i++)
37             {
38                 byte[] dataCompressed = null;
39                 byte[] buf = Encoding.UTF8.GetBytes(source);
40                 using (MemoryStream ms = new MemoryStream())
41                 {
42                     Stream s = new GZipStream(ms, CompressionMode.Compress);
43                     s.Write(buf, 0, buf.Length);
44                     s.Close();
45                     dataCompressed = (byte[])ms.ToArray();
46                 }
47             }
48             time.Stop();
49             Console.WriteLine("System_Compress\t\t压缩10000次,耗时:{0}毫秒。", time.ElapsedMilliseconds);
50         }
51 
52     }
53 }

运行结果:

另外,压缩率也基本一致。

代码中的Compress是一个扩展方法,来源于NewLife.Core的IOHelper中的方法。