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

后端 未结 9 1894
遥遥无期
遥遥无期 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 20:06

    Have you considered using the CCR since you are writing to separate files you can do everything in parallel (read and write) and the CCR makes it very easy to do this.

    static void Main(string[] args)
        {
            Dispatcher dp = new Dispatcher();
            DispatcherQueue dq = new DispatcherQueue("DQ", dp);
    
            Port offsetPort = new Port();
    
            Arbiter.Activate(dq, Arbiter.Receive(true, offsetPort,
                new Handler(Split)));
    
            FileStream fs = File.Open(file_path, FileMode.Open);
            long size = fs.Length;
            fs.Dispose();
    
            for (long i = 0; i < size; i += split_size)
            {
                offsetPort.Post(i);
            }
        }
    
        private static void Split(long offset)
        {
            FileStream reader = new FileStream(file_path, FileMode.Open, 
                FileAccess.Read);
            reader.Seek(offset, SeekOrigin.Begin);
            long toRead = 0;
            if (offset + split_size <= reader.Length)
                toRead = split_size;
            else
                toRead = reader.Length - offset;
    
            byte[] buff = new byte[toRead];
            reader.Read(buff, 0, (int)toRead);
            reader.Dispose();
            File.WriteAllBytes("c:\\out" + offset + ".txt", buff);
        }
    

    This code posts offsets to a CCR port which causes a Thread to be created to execute the code in the Split method. This causes you to open the file multiple times but gets rid of the need for synchronization. You can make it more memory efficient but you'll have to sacrifice speed.

提交回复
热议问题