Multi threaded file processing with .NET

后端 未结 6 540
栀梦
栀梦 2021-01-30 15:21

There is a folder that contains 1000s of small text files. I aim to parse and process all of them while more files are being populated into the folder. My intention is to multit

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 15:31

    Generally speaking, 1000 small files (how small, btw?) should not take six minutes to process. As a quick test, do a find "foobar" * in the directory containing the files (the first argument in quotes doesn't matter; it can be anything) and see how long it takes to process every file. If it takes more than one second, I'll be disappointed.

    Assuming this test confirms my suspicion, then the process is CPU-bound, and you'll get no improvement from separating the reading into its own thread. You should:

    1. Figure out why it takes more than 350 ms, on average, to process a small input, and hopefully improve the algorithm.
    2. If there's no way to speed up the algorithm and you have a multicore machine (almost everyone does, these days), use a thread pool to assign 1000 tasks each the job of reading one file.

提交回复
热议问题