PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label extension-methods. Show all posts
Showing posts with label extension-methods. Show all posts

Monday, October 31, 2022

[FIXED] Which method performs better: .Any() vs .Count() > 0?

 October 31, 2022     .net, .net-3.5, extension-methods, linq, performance     No comments   

Issue

in the System.Linq namespace, we can now extend our IEnumerable's to have the Any() and Count() extension methods.

I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension method instead of the .Count() > 0 extension method because the .Count() extension method has to iterate through all the items.

Secondly, some collections have a property (not an extension method) that is Count or Length. Would it be better to use those, instead of .Any() or .Count()?

yea / nae?


Solution

If you are starting with something that has a .Length or .Count (such as ICollection<T>, IList<T>, List<T>, etc) - then this will be the fastest option, since it doesn't need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any() to check for a non-empty IEnumerable<T> sequence.

For just IEnumerable<T>, then Any() will generally be quicker, as it only has to look at one iteration. However, note that the LINQ-to-Objects implementation of Count() does check for ICollection<T> (using .Count as an optimisation) - so if your underlying data-source is directly a list/collection, there won't be a huge difference. Don't ask me why it doesn't use the non-generic ICollection...

Of course, if you have used LINQ to filter it etc (Where etc), you will have an iterator-block based sequence, and so this ICollection<T> optimisation is useless.

In general with IEnumerable<T> : stick with Any() ;-p



Answered By - Marc Gravell
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, August 11, 2022

[FIXED] How to make Decimal conform LosslessStringConvertible in Swift 5?

 August 11, 2022     decimal, extension-methods, swift     No comments   

Issue

I want to convert Decimal to String in Swift 5.

Since I directly tried

String(aDecimalNumber)

Then Xcode gives error message saying:

Decimal needs to conform LosslessStringConvertible

so I add an extension looks like:

extension Decimal: LosslessStringConvertible {
    public init?(_ description: String) {


    }
}

But it won't work. My understanding is to extract the description, which is of String type, to a Decimal. But not sure how to do this?


Solution

Here is a possible implementation:

extension Decimal: LosslessStringConvertible {
    public init?(_ description: String) {
        guard let decimal = Decimal(string: description) else {
            return nil
        }
        self = decimal
    }
}


let d: Decimal? = Decimal("1e9")  //1000000000


Answered By - ielyamani
Answer Checked By - David Marino (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing