String Builder vs Lists

前端 未结 5 1931
野的像风
野的像风 2021-01-17 01:19

I am reading in multiple files in with millions of lines and I am creating a list of all line numbers that have a specific issue. For example if a specific field is left bla

相关标签:
5条回答
  • 2021-01-17 01:22

    My end goal is to out put a message like "Specific field is blank on 1-32, 40, 45, 47, 49-51

    If that's the end goal, no point in going through an intermediary representation such as a List<int> - just go with a StringBuilder. You will save on memory and CPU that way.

    0 讨论(0)
  • 2021-01-17 01:22

    StringBuilder serves your purpose so stick with that, if you ever need the line numbers you can easily change the code then.

    0 讨论(0)
  • 2021-01-17 01:30

    Is your output supposed to be human readable? If so, you'll hit the limit of what is reasonable to read, long before you have any performance/memory issues from your data structure. Use whatever is easiest for you to work with.

    If the output is supposed to be machine readable, then that output might suggest an appropriate data structure.

    0 讨论(0)
  • 2021-01-17 01:32

    As others have pointed out, I would probably use StringBuilder. The List may have to resize many times; the new implementation of StringBuilder does not have to resize.

    0 讨论(0)
  • 2021-01-17 01:38

    Depends on how you can / want to break the code up.

    Given you are reading it in line order, not sure you need a list at all. Your current desired output implies that you can't output anything until the file is completely scanned. The size of the file suggests a one pass`analysis phase would be a good idea as well, given you are going to use buffered input as opposed to reading the entire thing into memory.

    I'd be tempted with an enum to describe the issue e.g Field??? is blank and then use that as the key a dictionary of string builders.

    As a first thought anyway

    0 讨论(0)
提交回复
热议问题