Hi suppose these 2 methods:
private List<IObjectProvider> GetProviderForType(Type type)
{
List<IObjectProvider> returnValue = new List<IObjectProvider>();
foreach (KeyValuePair<Type, IObjectProvider> provider in _objectProviders)
{
if ((provider.Key.IsAssignableFrom(type) ||
type.IsAssignableFrom(provider.Key)) &&
provider.Value.SupportsType(type))
{
returnValue.Add(provider.Value);
}
}
return returnValue;
}
private IEnumerable<IObjectProvider> GetProviderForType1(Type type)
{
foreach (KeyValuePair<Type, IObjectProvider> provider in _objectProviders)
if ((provider.Key.IsAssignableFrom(type) ||
type.IsAssignableFrom(provider.Key)) &&
provider.Value.SupportsType(type))
yield return provider.Value;
}
Which one is quicker? When I look at the first method, I see that the memory is allocated for List, what in my opinion it's not needed. The IEnumerable method seems to be quicker to me.
For instance, suppose you call
int a = GetProviderForType(myType).Count;
int b = GetProviderForType1(myType).Count();
Now, another issue is, is there a performance difference between these 2 above?
What do you think?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
In this particular case, using the IEnumerable<T>
form will be more efficient, because you only need to know the count. There's no point in storing the data, resizing buffers etc if you don't need to.
If you needed to use the results again for any reason, the List<T>
form would be more efficient.
Note that both the Count()
extension method and the Count
property will be efficient for List<T>
as the implementation of Count()
checks to see if the target sequence implements ICollection<T>
and uses the Count
property if so.
Another option which should be even more efficient (though only just) would be to call the overload of Count
which takes a delegate:
private int GetProviderCount(Type type)
{
return _objectProviders.Count(provider =>
(provider.Key.IsAssignableFrom(type)
|| type.IsAssignableFrom(provider.Key))
&& provider.Value.SupportsType(type));
}
That will avoid the extra level of indirections incurred by the Where
and Select
clauses.
(As Marc says, for small amounts of data the performance differences will probably be negligible anyway.)
An important part of this question is "how big is the data"? How many rows...
For small amounts of data, list is fine - it will take negligible time to allocate a big enough list, and it won't resize many times (none, if you can tell it how big to be in advance).
However, this doesn't scale to huge data volumes; it seems unlikely that your provider supports thousands of interfaces, so I wouldn't say it is necessary to go to this model - but it won't hurt hugely.
Of course, you can use LINQ, too:
return from provider in _objectProviders
where provider.Key.IsAssignableFrom(type) ...
select provider.Value;
This is also the deferred yield
approach under the covers...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With