*(00)*

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  613 随笔 :: 0 文章 :: 45 评论 :: 159万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
本帖来源:http://www.cnblogs.com/lykbk/archive/2013/08/15/3259045.html

最近一直在优化项目的性能,就在前几天找到了一些资料,终于有方案了,那就是压缩数据。

一丶前端和后端的压缩和解压缩流程

   

 

二丶优点和缺点

  优点:①字符串的压缩率能够达到70%-80%左右

     ②字符串数量更少了

  缺点:①CPU的开销会大一点,不过在可承受范围之内

 

三丶通过标记来说明数据是否压缩过

  这个一开始是没有想到,是经理提醒我的,通过在数据(字符串)的最前端添加一个标记来说明数据是否压缩过。

  因为只有当数据大于一定数量的时候才进行压缩操作。

 

四丶测试(简单的测试)

  环境:1.启动500线程 (相当于500个客户端)

        2.500个线程同时调用

        3.服务端和客户端在同一个局域网的两台PC机

        4.未压缩的字符串长度为65000,压缩之后长度为8400 

测试结果:

 

结果:其实测试的结果很明显了,通过压缩数据来减少网络数据量的传输,确实可以提高速度。

 

五丶代码

复制代码
 1 public class CompressionHelper
 2     {
 3         public static string Compress(string value)
 4         {
 5             string result = string.Empty;
 6             try
 7             {
 8                 byte[] buffer = Encoding.UTF8.GetBytes(value);
 9                 using (MemoryStream memoryStream = new MemoryStream())
10                 {
11                     using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true))
12                     {
13                         deflateStream.Write(buffer, 0, buffer.Length);
14                     }
15                     result = Convert.ToBase64String(memoryStream.ToArray());
16                 }
17             }
18             catch (InvalidDataException invalidData)
19             {
20                 //Log21             }
21             catch (Exception exception)
22             {
23                 //Log25             }
24             return result;
25         }
26 
27         public static string Decompress(string value)
28         {
29             string result = string.Empty;
30             try
31             {
32                 byte[] bytes = Convert.FromBase64String(value);
33                 using (MemoryStream outStream = new MemoryStream())
34                 {
35                     using (MemoryStream inStream = new MemoryStream(bytes))
36                     {
37                         using (DeflateStream deflateStream = new DeflateStream(inStream, CompressionMode.Decompress, true))
38                         {
39                             int readLength = 0;
40                             byte[] buffer = new byte[1024];
41                             while ((readLength = deflateStream.Read(buffer, 0, buffer.Length)) > 0)
42                             {
43                                 outStream.Write(buffer, 0, readLength);
44                             }
45                         }
46                     }
47                     result = Encoding.UTF8.GetString(outStream.ToArray());
48                 }
49             }
50             catch (InvalidDataException invalidData)
51             {
52                //Log55             }
53             catch (Exception exception)
54             {
55                //Log59             }
56             return result;
57         }
复制代码

 

分类: android相关
 
posted on   *(00)*  阅读(302)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示