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

  • SEARCH
  • Home
  • 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 7713197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:48:49+00:00 2026-06-01T01:48:49+00:00

I’m using some not optimal code written by me… :-| I have following code:

  • 0

I’m using some not optimal code written by me… 😐

I have following code:

string fmtLine = "";
            string[] splitedFmtLine;
            int counterFMTlines = 0;

            foreach (string fmtF in fmtFiles)
            {
                using (StreamReader sr = new StreamReader(fmtF))
                {
                    while ((fmtLine = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(counterFMTlines++);
                        foreach (L3Message message in rez)
                        {
                            splitedFmtLine = Regex.Split(fmtLine, "\t");

                            if (message.Time == splitedFmtLine[0])
                            {
                                message.ScramblingCode = splitedFmtLine[7];      
                            }
                        }
                    }
                }
            }

I tested this code when List was empty and there was only one file (tab delimited, 280000 lines), and even then it took lifetime (1 min) to go through all 280000 lines of my file. That means that execution skipped foreach loop where is my List of myObjs.

I cannot understand why it took so long?

As example, I was filling my List of myObjs (tree hierarchy) with different text file (source file) but bigger than this tab delimited(tab delimited: 16MB, source file: 36MB) and it took only second versus this 1 minute.

  • 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-06-01T01:48:51+00:00Added an answer on June 1, 2026 at 1:48 am

    Apart from the problem writing to the console, you also have a O(m*n) runtime for n being the number of lines in the file and m being the number of messages. This is bad if m or n is big. You can reduce this to an O(n) operation by using a Dictionary instead and eliminating the inner loop.

    You can put your messages in a Dictionary, using the Time as a key. In the loop you only have to ask the dictionary for the messages at a specific time:

            string fmtLine = "";
            string[] splitedFmtLine;
            int counterFMTlines = 0;
    
            var messageTimes = new Dictionary<string, LinkedList<L3Message>>();
            foreach (L3Message message in rez)
            {
                LinkedList<L3Message> list=null;
                messageTimes.TryGetValue(message.Time, out list);
    
                list = list ?? new LinkedList<L3Message>();
    
                list.AddLast(message);
                messageTimes[message.Time] = list;
            }
    
            foreach (string fmtF in fmtFiles)
            {
                using (StreamReader sr = new StreamReader(fmtF))
                {
                    while ((fmtLine = sr.ReadLine()) != null)
                    {
                        //Console.WriteLine(counterFMTlines++);
                        splitedFmtLine = fmtLine.Split('\t');
    
                        LinkedList<L3Message> messageList = null;
                        messageTimes.TryGetValue(splitedFmtLine[0], out messageList);
    
                        if(messageList != null)
                        {
                            foreach (var message in messageList)
                            {
                                message.ScramblingCode = splitedFmtLine[7];                                
                            }
                            messageTimes.Remove(splitedFmtLine[0]); //see comments
                        }
    
                        if(messageTimes.Count==0) break; //see comments
                    }
                }
                if(messageTimes.Count==0) break; //see comments
            } 
    

    This should be super fast.

    Edit: I changed the example so that it supports cases where there is more than one message for one time.

    Edit2: I added an optimization based on the fact that message time and ScramblingCode always correlate (see comments).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.