哪个更快? IEnumerable或List With Example。 [英] which is Faster? IEnumerable or List With Example.

查看:71
本文介绍了哪个更快? IEnumerable或List With Example。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public IEnumerable<KeyValuePair<int, string>> GetCity()
{
IEnumerable<KeyValuePair<int, string>> CityList = new List<KeyValuePair<int, string>>();
IEnumerable<City_Mast> objCity = ent.City_Mast.ToList();
CityList = objCity.Select(f => new KeyValuePair<int, string>(f.cid, f.city));
return CityList;
}

 public List<KeyValuePair<int, string>> GetCity()
{
List<KeyValuePair<int, string>> CityList = new List<KeyValuePair<int, string>>();
List<City_Mast> objCity = ent.City_Mast.ToList();
CityList = objCity.Select(f => new KeyValuePair<int, string>(f.cid, f.city)).ToList();
return CityList;
}





我在上面的示例中使用了IEnumerable和List Both。

有谁能告诉我哪一个更快?

什么时候使用IEnumerable和list?



I Used IEnumerable and List Both in Above Example.
Can Anyone tell me which one is faster?
when to use IEnumerable and list?

推荐答案

一个List是IEnumerable,通过中介 - 如果你浏览参考资料来源:

A List is an IEnumerable, via an intermediary - if you browse the Reference Source:
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>



和IList是


And a IList is

public interface IList<T> : ICollection<T>

并且:

And:

public interface ICollection<T> : IEnumerable<T>



因此实际使用它们时它们是相同的,速度差异将取决于你正在做什么。



对于你的代码,第一个代码会明显更快,因为它没有执行选择 - 它只是设置它以便以后执行。与执行选择的List代码不同,因为使用ToList折叠操作并且实际上要求迭代集合。



如果你想知道什么是最快的,然后测试它!

System.Diagnostics中有一个Stopwatch类用于此目的。


So in effect they are the same when it comes to actually using them, and speed difference will depend on exactly what you are doing.

In the case of your code, the first one will be significantly faster, because it doesn;t execute the Selects - it just sets it up for later execution. Unlike your List code which does execute the selects because the use of ToList "collapses the operation" and actually requires that the collection is iterated.

If you want to know what is fastest, then test it!
There is a Stopwatch class in System.Diagnostics for just that purpose.


这取决于你的调用代码的作用。 IEnumerable是一次性的仅向前机制,而List在内存中,可以根据需要自由导航。如果调用代码遍历每个城市一次(即在下拉列表中显示值),那么它既不在这里也不在那里。如果只显示一个或一些城市,那么IEnumerable会更好,如果城市多次运行,或者重新排序,或者你做了多次选择过滤等,那么List就更好了。
It depends what your calling code does. IEnumerable is a one-shot, forward-only mechanism, whereas a List is in memory and can be freely navigated as much as needed. If the calling code runs through every city once (ie showing the values in a drop down) then it's neither here nor there. If only one, or some cities are shown then IEnumerable is better, if the cities are ran through multiple times, or re-ordered, or have you doing multiple selects\filters etc then the List is better.


这么多无用的答案......



让我解释主要问题。



1)如果你只从IEnumerable或List中迭代元素一次,那么两者都是相同的,没有差别。



2)如果你多次迭代元素,或者你多次获得一个元素,因此,IEnumerable将会很慢。



因为IEnumerable它不包含结果,所以它创建了一个新的结果实例它被使用了。这意味着,当您多次访问同一个元素时,每个元素都是不同的对象!
so many useless answers...

let me explain main issue.

1) if you iterate elements from IEnumerable or List only one time, so both are same, no differences.

2) if you iterate elements many times, or if you get an one element for multiple times, so, IEnumerable will be slow.

Because of IEnumerable it does not contain result, so that it creates an new instance of result when it's used. It means, when you access an same element multiple times, each will be different object!


这篇关于哪个更快? IEnumerable或List With Example。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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