C#高级:程序查询写法性能优化提升策略(附带Gzip算法示例)

一、基本要求

bash 复制代码
1.按需查询,不要的字段不查
2.缓存优先,内存比磁盘读写快(Redis也不要放太多的数据,按需存储,不然读写也慢)
3.线程并行,最后await输出所有结果
4.批量优先,插入或更新使用数据库批量操作
5.压缩算法,传输大量重复文本,避免网络大开销
6.耗时长操作推入消息队列,慢慢消费
7.循环内部短流程,不要for循环里面写耗时操作例如查库、读写文件
8.非必要不递归
9.非必要不加锁(尽量不要修改静态字段,尽量用实例字段)

二、Gzip算法

cs 复制代码
public class Program
{
    /*
        简介:Gzip是一种基于DEFLATE算法的无损数据压缩算法,广泛应用于文件压缩和网络数据传输。
        算法:DEFLATE 算法 =  LZ77字典编码 + 霍夫曼编码
        适用场景:大量重复内容文本、长文本
     */


    /// <summary>
    /// 压缩
    /// </summary>
    public static string CompressString(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        byte[] compressedData = null;
        using (var memoryStream = new MemoryStream())
        {
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
            }

            memoryStream.Position = 0;

            compressedData = new byte[memoryStream.Length];
            memoryStream.Read(compressedData, 0, compressedData.Length);
        }
        var gZipBuffer = new byte[compressedData.Length + 4];
        Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
        return Convert.ToBase64String(gZipBuffer);
    }

    /// <summary>
    /// 解压
    /// </summary>
    public static string DecompressString(string compressedText)
    {
        byte[] gZipBuffer = Convert.FromBase64String(compressedText);
        using (var memoryStream = new MemoryStream())
        {
            int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
            memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

            var buffer = new byte[dataLength];

            memoryStream.Position = 0;
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
            {
                gZipStream.Read(buffer, 0, buffer.Length);
            }

            return Encoding.UTF8.GetString(buffer);
        }
    }

    static async Task Main(string[] args)
    {
        // 测试1:高度重复的文本
        var repetitiveText = new string('A', 1000) + new string('B', 1000) + new string('C', 1000);
        Console.WriteLine($"重复文本原始长度: {repetitiveText.Length}");
        var compressed1 = CompressString(repetitiveText);
        Console.WriteLine($"重复文本压缩后: {compressed1.Length}");
        Console.WriteLine($"压缩比: {(double)compressed1.Length / repetitiveText.Length:P}");

        // 测试2:你的原始文本
        var originalText = ""ConsoleApp1.exe"(CoreCLR: clrhost): 已加载...";
        Console.WriteLine($"\n原始文本长度: {originalText.Length}");
        var compressed2 = CompressString(originalText);
        Console.WriteLine($"原始文本压缩后: {compressed2.Length}");
        Console.WriteLine($"压缩比: {(double)compressed2.Length / originalText.Length:P}");

        // 测试3:JSON数据(通常压缩效果好)
        var jsonText = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}".Repeat(50);
        Console.WriteLine($"\nJSON文本长度: {jsonText.Length}");
        var compressed3 = CompressString(jsonText);
        Console.WriteLine($"JSON文本压缩后: {compressed3.Length}");
        Console.WriteLine($"压缩比: {(double)compressed3.Length / jsonText.Length:P}");
    }
}

三、相关技巧

C#高级:提升数据库查询效率的优化策略与方法_c# 分表查询-CSDN博客

相关推荐
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹3 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her13 天前
并查集-听课笔记
笔记·算法·并查集
霍霍的袁3 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio