Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:27:09+00:00 2026-05-24T17:27:09+00:00

I wrote a small algorithm using LINQ to read in a bunch of files

  • 0

I wrote a small algorithm using LINQ to read in a bunch of files (about 30mb) and store them in memory, currently it takes about a minute for the program to finish reading in all files, however I need this process to only take a few seconds.

Code:

List<ClimateDailyData> dailyData = new List<ClimateDailyData>();
if (File.Exists(FileName))
{
    StreamReader reader = new StreamReader(FileName);
    try
    {
        List<string[]> lines = 
            Regex.Split(reader.ReadToEnd(), Environment.NewLine)
                .Where(l => !String.IsNullOrWhiteSpace(l) && !String.IsNullOrEmpty(l))
                .Select(l => l.Trim().Split(new char[] { ' ', '\t' })
                .Where(f => !String.IsNullOrWhiteSpace(f) && !String.IsNullOrEmpty(f))
                .Select(f => f.Trim())
                .ToArray())
                .ToList();
        Latitude = double.Parse(lines[0][0]);
        Longitude = double.Parse(lines[0][1]);
        lines.RemoveRange(0, 2);
        foreach (string[] fields in lines)
        {
            ClimateDailyData dayData = new ClimateDailyData();
            dayData.DayDate = DateTime.ParseExact(fields[0], "yyyyMMdd", 
                                   CultureInfo.InvariantCulture, DateTimeStyles.None);
            dayData.MaxTemp = double.Parse(fields[2]);
            dayData.MinTemp = double.Parse(fields[3]);
            dayData.Rain = double.Parse(fields[4]);
            dayData.Pan = double.Parse(fields[5]);
            dailyData.Add(dayData);
        }
    }
    finally { reader.Close(); }
}
SetValue(() => DailyData, dailyData);

Can anyone sugest how I could speed this code up? The majority of the time seems to be involved with parsing the individual file fields (especially the date field).

However if it cannot be sped up I will simply make it so each individual file is loaded as required.

Thanks,
Alex.

EDIT:
Also I decided to just store a few fields from each file rather then all file data and then load the rest of the data in a seperate thread and make it avaiable to the user as it finishes loading.

So now it only takes 2.7seconds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:27:10+00:00Added an answer on May 24, 2026 at 5:27 pm

    As noted in comments, it’s an odd way of reading lines – but I wouldn’t use File.ReadAllLines, I’d use File.ReadLines if you’re using .NET 4 – that only reads one line at a time.

    Beyond that – you definitely don’t need to call ToArray and ToList… I’d also use Select and ToList with Skip to create dailyData. Also, String.IsNullOrWhiteSpace already returns false if the string is empty, so you can remove those calls.

    After splitting, you’re currently trimming and removing any empty/whitespace entries. You can remove empty entries with StringSplitOptions.RemoveEmptyEntries and if you’re confident that the only whitespace in a line would be space or tab, you then don’t need to worry about trimming or anything else. If you have other whitespace which needs trimming, it could still be a problem – but I doubt that’s the case. One big benefit of that is that you can use the array returned by Split directly, rather than copying it to another collection.

    private static readonly char[] Delimiters = { ' ', '\t' };
    
    ...
    
    List<ClimateDailyData> dailyData;
    if (!File.Exists(FileName))
    {
        dailyData = new List<ClimateDailyData>();
    }
    else
    {
        dailyData = File.ReadLines(FileName)
           .Where(l => !String.IsNullOrWhiteSpace(l))
           .Select(l => l.Trim()
                         .Split(Delimiters, StringSplitOptions.RemoveEmptyEntries))
           .Select(fields => new ClimateDailyData
                   {
                       DayDate = DateTime.ParseExact(fields[0], "yyyyMMdd",
                                                     CultureInfo.InvariantCulture,
                                                     DateTimeStyles.None),
                       MaxTemp = double.Parse(fields[2]),
                       MinTemp = double.Parse(fields[3]),
                       Rain = double.Parse(fields[4]),
                       Pan = double.Parse(fields[5])
                   })
           .ToList();
    }
    SetValue(() => DailyData, dailyData);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java
I wrote a small application using iPhone 3.x sdk. The app works well on
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I wrote a small PHP application that I'd like to distribute. I'm looking for
We wrote a small Windows class library that implements extension methods for some standard
I wrote a small sample of code in C# to capture selected text from
I recently wrote a small tool to generate a class for each tier I
Right now I'm developing mostly in C/C++, but I wrote some small utilities in
I have several small open-source projects that I wrote. All my attempts to find
I am bit confused about the difference between the usage of std::remove algorithm. Specifically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.