C# 每日学习

计算代码块执行时间

{
    // 创建一个 Stopwatch 实例
    Stopwatch stopwatch = new Stopwatch();

    // 启动计时器
    stopwatch.Start();

    // 需要测量时间的代码段
    PerformTask();

    // 停止计时器
    stopwatch.Stop();

    // 获取代码段的执行时间
    TimeSpan elapsedTime = stopwatch.Elapsed;

    // 输出执行时间
    Console.WriteLine("代码段执行时间: " + elapsedTime.TotalMilliseconds + " 毫秒");
}

List分批处理

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 示例 List
        List<int> dataList = new List<int>();
        // 假设 dataList 已经被填充数据

        int batchSize = 10000;
        int totalCount = dataList.Count;

        for (int i = 0; i < totalCount; i += batchSize)
        {
            int currentBatchSize = Math.Min(batchSize, totalCount - i);
            List<int> batch = dataList.GetRange(i, currentBatchSize);

            // 处理当前批次数据
            ProcessBatch(batch);
        }
    }

    static void ProcessBatch(List<int> batch)
    {
        // 处理批次数据的逻辑
        Console.WriteLine($"Processing batch of size: {batch.Count}");
    }
}
posted @ 2024-08-27 23:51  NAfei  阅读(2)  评论(0编辑  收藏  举报