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 6683457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:26+00:00 2026-05-26T04:49:26+00:00

Here’s a link to the previous question I had, which led me to this

  • 0

Here’s a link to the previous question I had, which led me to this one.
C# Nested foreach loop optimization

There is still high computation time, and I’m not sure what the cause is.

object foo;
List<string> StringList = new List<string>(); // Populated by previous code
Dictionary<string, Type> assemblyTypes = RandomAssembly.GetTypes().ToDictionary(t => t.Name, t => t);

  foreach (String name in StringList)
  {                     
    if (assemblyTypes.ContainsKey(name))
    {
      // Create an instance of the matching class and add it
      // to the appropriate lists.
      Type at = assemblyTypes[name];
      foo = Activator.CreateInstance(at);       
      ArbitraryList1.Add(foo);
    }
  }    
  • 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-26T04:49:27+00:00Added an answer on May 26, 2026 at 4:49 am

    If the computation time is noticeably slow I guess you’re calling into this code a lot and you’re newing up a lot of objects that you don’t know much about at compile time.

    Instead of keeping the classes in a Dictionary<string, type> and call CreateInstance you want to keep a dictionary of their constructors Dictionary<string, ConstructorInfo> so you can call them directly without having to look for it every time with reflection.

    That way you can just call AssemblyConstructors[Name].Invoke() to create a new instance of the class.

    This way you only have to use reflection once to find the constructors.

    // keep a dictionary of constructors (instead of the types)
    var Constructors = new Dictionary<string, ConstructorInfo>();            
    
    // add this class to this dictionary
    Type t = typeof(SomeClass);            
    string name = t.Name;
    Constructors[name] = t.GetConstructors()[0]; // use reflection only once here, afterwards we reuse the reflected info
    
    // new up an instance 
    var o = Constructors[name].Invoke(new object[] {}); 
    

    I think the first constructor will be the parameterless one. Else try something like t.GetConstructors().Where(x => x.GetParameters().Count() == 0).First();
    This is the fastest easy way I know because apparently you can’t get a delegate to a constructor.

    If you write the classes you’re newing up yourself, you can have a common base class or interface with a method that creates one, that way you can keep a delegate to that constructor, that’s even faster.

    This post also has some interesting ideas about this that take optimization much further. If you want to do this fast, you can. Almost as fast as just calling new KnownClass()

    Good luck,

    GJ

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

Sidebar

Related Questions

Here a simple question : What do you think of code which use try
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here's some of my production code (I had to force line breaks): task =
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
here is a link to how my APC is running : [removed] As you
Here is what I want to do. Use this HTML line and have the
Here's the link: www.mchenry.edu/maps/google.asp Why won't the location balloon display correctly? Also, the pop-up
Here's my situation. I have a DotNetNuke application. I want to link to an
Here is the code in which i am having the problem- <!DOCTYPE html> <html>

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.