高计算时间 [英] High computation time

查看:117
本文介绍了高计算时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前的问题的一个链接,这导致了我的这个问题。
嵌套的foreach循环优化



计算时间依然很长,我不确定原因是什么。

  object foo ; 
列表< string> StringList = new List< string>(); //由前面的代码填充
Dictionary< string,Type> assemblyTypes = RandomAssembly.GetTypes()。ToDictionary(t => t.Name,t => t);

foreach(StringList中的字符串名称)
{
if(assemblyTypes.ContainsKey(name))
{
//创建一个匹配的实例类并将其添加
//到适当的列表中。
键入at = assemblyTypes [name];
foo = Activator.CreateInstance(at);
ArbitraryList1.Add(foo);



$ div $解析方案

如果计算时间明显很慢我想你已经调用了很多这个代码,并且在编译的时候你正在创建许多不太了解的对象。

不要将类保存在 Dictionary< string中,输入> 并调用 CreateInstance 你想保留一个字典的构造函数 Dictionary< string,ConstructorInfo> ,所以你可以直接调用它们而不必每次都查找它反射。这样你可以调用AssemblyConstructors [Name] .Invoke()来创建一个新的类实例。



这样你只需要使用一次反射就可以找到构造函数。

  //保存构造函数的字典(而不是类型)
var构造函数=新字典< string,ConstructorInfo>();

//将这个类添加到这个字典
类型t = typeof(SomeClass);
string name = t.Name;
Constructors [name] = t.GetConstructors()[0]; //在这里只使用反射一次,之后我们再次使用反射信息

//新建一个实例
var o =构造函数[name] .Invoke(new object [] {});

我认为第一个构造函数是无参数的。或者尝试像 t.GetConstructors()这样的方法。 b这是我知道的最简单的方法,因为显然你不能将委托给一个构造函数

如果你自己编写新的类,你可以有一个通用的基类或接口用一个方法创建一个,这样你可以保留一个委托给那个构造函数,那就更快了。

这篇文章也有一些有趣的想法,进一步优化。如果你想这样做,你可以。几乎就像调用 new KnownClass()



祝你好运

GJ

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);
    }
  }    

解决方案

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

这篇关于高计算时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆