List.OfType() 速度,替代数据结构

发布于 2024-10-08 00:07:52 字数 853 浏览 5 评论 0 原文

看看这段代码。

interface ILoader
{
}

interface ILoader<T>: ILoader
{
    T Load();
}

class CarLoader: ILoader<Car>
{
    ...
}

class TrainLoader: ILoader<Train>
{
    ...
}

class Container
{
     List<ILoader> loaders = new ILoader[] { new CarLoader(), new TrainLoader()};

     public T Load<T>()
     {
         // Finding right loader
         var loader = loaders.OfType<ILoader<Car>>.FirstOrDefault();
         return loader.Load();
     }
}

我有大约 100 个装载机,我需要装载很多火车、汽车等。我认为装载机列表非常慢(具有 OfType() 线性复杂度??),什么你建议使用而不是列表? DictionaryHashtable 还是 HashSet?例如,使用 hashset.OfType>() 的速度有多快,与 list 相同还是更快?

Take a look at this code.

interface ILoader
{
}

interface ILoader<T>: ILoader
{
    T Load();
}

class CarLoader: ILoader<Car>
{
    ...
}

class TrainLoader: ILoader<Train>
{
    ...
}

class Container
{
     List<ILoader> loaders = new ILoader[] { new CarLoader(), new TrainLoader()};

     public T Load<T>()
     {
         // Finding right loader
         var loader = loaders.OfType<ILoader<Car>>.FirstOrDefault();
         return loader.Load();
     }
}

I've got about 100 of loaders and I need to load a lot of Trains, Cars, etc. I think that List of loaders is very slow (has OfType() linear complexity??), what do you suggest to use instead of list? Dictionary<Type,ILoader> or Hashtable<Type,ILoader> or HashSet<ILoader>? How fast would be for example to use hashset.OfType<ILoader<Car>>(), same as list or faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-10-15 00:07:52

构建一个 Dictionary 并使用加载器填充它。然后您可以这样做:

ILoader<T> loader = (ILoader<T>) loaderDictionary[typeof(T)];

另一方面,如果您只有 100 个项目需要查看,即使是线性扫描也不会花费很长时间。您是否真正对真实的使用情况进行了基准测试并发现这是您的瓶颈?

Build a Dictionary<Type, ILoader> and populate it with the loaders. Then you can just do:

ILoader<T> loader = (ILoader<T>) loaderDictionary[typeof(T)];

On the other hand, if you've only got 100 items to look through, even a linear scan isn't exactly going to take long. Have you actually benchmarked a real usage situation and found this to be your bottleneck?

聆听风音 2024-10-15 00:07:52

Enumerable.OfType 扩展方法确实运行在线性时间内,它可能足够快以满足您的目的。除非您已经测量了性能并确定需要优化它,否则不要对代码进行微优化。

您应该首先考虑设计的适用性,而不是专注于性能。一个好的设计通常不需要检查对象的类型 - 您需要的信息应该可以通过其他方式获得。例如,在这种情况下,您可能想询问每个加载器是否能够通过将对象传递给 CanLoad 方法并返回 truefalse< 来加载该对象。 /代码>。这将使您的设计更加灵活。

Loader loader = loaders.First(x => x.CanLoad(myObject));

现在您可以拥有可以加载多种类型对象的加载器。

如果您每次都想要一个新的 Loader 并且想要一对一的映射,另一种选择是还要求对象本身创建一个合适的加载器:

Loader loader = myObject.CreateLoader();

每个类都可以不同地实现 CreateLoader ,以便您获得适合您的对象的正确类型的加载器。通过利用多态性,无需询问对象的类型即可实现此目的。

The Enumerable.OfType extension method does run in linear time and it is probably fast enough for your purposes. Don't micro-optimizate your code unless you have measured the performance and are sure that you need to optimize it.

Rather than concentrating on the performance you should first consider the suitability of your design. A good design in general does not need to inspect the types of the object - the information you need should be available in other ways. In this case for example you might want to ask if each loader is capable of loading an object by passing that object to a CanLoad method and returning true or false. This will make your design more flexible.

Loader loader = loaders.First(x => x.CanLoad(myObject));

Now you can have loaders that can load multiple types of objects.

If you want a new Loader each time and you want a one-to-one mapping another option is to also ask the object itself to create a suitable loader:

Loader loader = myObject.CreateLoader();

Each class can implement CreateLoader differently so that you get a Loader of the correct type for your object. By taking advantage of polymorphism this works without ever needing to ask an object what type it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文