You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中对大缓冲区分区,获取起始索引、结束索引及长度?

How to Split a Large Buffer into Equal-ish Partitions

Let's walk through how to split your buffer into the required number of partitions, focusing on getting the start index, end index, and length for each segment—perfect for writing data to a single large buffer like a custom buffer pool.

Core Calculation Logic

The goal is to split the buffer as evenly as possible. When the total length isn't perfectly divisible by the number of partitions, a small number of partitions will be 1 byte longer than the rest. Here's the step-by-step approach:

  1. Calculate the base length for most partitions: base_length = total_length // num_partitions (integer division)
  2. Find the remainder: remainder = total_length % num_partitions (this tells us how many partitions need to be 1 byte longer)
  3. The first remainder partitions will have a length of base_length + 1; the rest will use base_length
  4. Iterate through each partition to compute start/end indices:
    • The first partition starts at 0
    • For each subsequent partition, start index = previous partition's end index + 1
    • End index = start index + partition length - 1

Example Walkthrough (Your Total Length = 14, 5 Partitions)

Let's verify this with your sample case to confirm it works:

  • Total length: 14, partitions: 5
  • base_length = 14 // 5 = 2
  • remainder = 14 % 5 = 4 → first 4 partitions are length 3, last is 2

Which matches your expected output exactly:

  • Partition 1: Start=0 || End=2 || Length=3
  • Partition 2: Start=3 || End=5 || Length=3
  • Partition 3: Start=6 || End=8 || Length=3
  • Partition 4: Start=9 || End=11 || Length=3
  • Partition 5: Start=12 || End=13 || Length=2

Applying to Your Buffer (Size = 1048576, 5 Partitions)

Now let's compute this for your actual buffer:

  • Total length: 1048576, partitions: 5
  • base_length = 1048576 // 5 = 209715
  • remainder = 1048576 % 5 = 1 → only the first partition is 1 byte longer, others use the base length

Here are your exact partition details:

  • Partition 1: Start=0 || End=209715 || Length=209716
  • Partition 2: Start=209716 || End=419430 || Length=209715
  • Partition 3: Start=419431 || End=629145 || Length=209715
  • Partition 4: Start=629146 || End=838860 || Length=209715
  • Partition 5: Start=838861 || End=1048575 || Length=209715

Quick Automation Script (Python Example)

If you want to generate these partitions programmatically, here's a simple script:

def split_buffer(total_length, num_partitions):
    base_len = total_length // num_partitions
    remainder = total_length % num_partitions
    partitions = []
    current_start = 0
    for i in range(num_partitions):
        # Determine length for current partition
        length = base_len + 1 if i < remainder else base_len
        current_end = current_start + length - 1
        # Store partition details
        partitions.append({
            "start": current_start,
            "end": current_end,
            "length": length
        })
        # Move to next partition's start
        current_start = current_end + 1
    return partitions

# Test with your buffer
buffer_size = 1048576
num_parts = 5
result = split_buffer(buffer_size, num_parts)
for idx, part in enumerate(result, 1):
    print(f"Partition {idx}: Start={part['start']} || End={part['end']} || Length={part['length']}")

内容的提问来源于stack exchange,提问作者Robert Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:39:59