How to write super-fast file-streaming code in C#?

后端 未结 9 1910
遥遥无期
遥遥无期 2020-11-28 19:11

I have to split a huge file into many smaller files. Each of the destination files is defined by an offset and length as the number of bytes. I\'m using the following code:<

9条回答
  •  情话喂你
    2020-11-28 19:59

    You shouldn't re-open the source file each time you do a copy, better open it once and pass the resulting BinaryReader to the copy function. Also, it might help if you order your seeks, so you don't make big jumps inside the file.

    If the lengths aren't too big, you can also try to group several copy calls by grouping offsets that are near to each other and reading the whole block you need for them, for example:

    offset = 1234, length = 34
    offset = 1300, length = 40
    offset = 1350, length = 1000
    

    can be grouped to one read:

    offset = 1234, length = 1074
    

    Then you only have to "seek" in your buffer and can write the three new files from there without having to read again.

提交回复
热议问题