-7

i want to completely remove foreach.

here are my code

foreach (var user in elite.Users)
{
    if (user.UserName==txtuser.Text && user.UserPWD==txtpass.Text)
    {                    
        MainForm mainForm = new MainForm();
        mainForm.ShowDialog();
        Hide();
        Close();
    }
}
bahi
  • 1
  • 8
  • 2
    When you're creating and showing dialogs, I don't think any potential overhead from foreach is an issue. Can you help us understand where you got the idea that foreach is so terribly slow? Do you have any documentation of that, or any code which demonstrates it? I ask because I've often used foreach and it isn't slow for me. Besides, if LINQ is doing anything which behaves like a loop, that means there is an actual loop there, in some method you're calling. – 15ee8f99-57ff-4f92-890c-b56153 Oct 01 '19 at 19:04
  • 5
    Why do you believe that LINQ expressions would be higher speed than a `foreach`? How do you think they're implemented? Furthermore, LINQ is for *querying* (it's what the `Q` means), not side-effects like this. – madreflection Oct 01 '19 at 19:06
  • Rather than a new, yet identical form for each user, consider reusing a single form instance for each user – Ňɏssa Pøngjǣrdenlarp Oct 01 '19 at 19:14
  • i have used linq expression to make some calculations after retrieving data from data base it was about 10 times faster than for loop. – bahi Oct 01 '19 at 19:56
  • `for` is faster than `foreach` is faster than `LINQ` for LINQ to Objects. For LINQ to EF / EF Core / SQL, LINQ may be faster than `foreach` depending on the nature of the query, and the type and speed of the database server, as the query is either executed on the database side (EF/EF Core 3/SQL) or split between the database server and the client (EF Core < 3). You can't say in general one is faster than another, but if you are not using a complex database query to a fast server, LINQ will be slower than `foreach`. – NetMage Oct 01 '19 at 20:09
  • Assuming `elite` is a database context, and you are using LINQ to some database (then update your question), then you should be using LINQ to query the database to retrieve the fewest records possible, not a `foreach` to retrieve and scan every record in the database. See `Queryable.Where` and read some LINQ beginner's guides. – NetMage Oct 01 '19 at 20:12

2 Answers2

2

Under the hood, Linq or Language Integrated Query is (almost) the same as executing a foreach.

LINQ is not faster, and can arguably be slower than a foreach. It is used a lot, because it can simplify and shorten code.

If you want to improve the performance of your iteration, think about adding break or continue statements where possible.

You can also make an iteration faster by using TLP (Task Parallel Library) Or PLINQ (Parallel LINQ), which use multi-threading.

Using multi-threading can in some (and I'd even say most) situations be slower than executing the iteration synchronized. It also causes your code to be much more complex, and is often the source of many bugs in applications if not understood.

Joas
  • 1,796
  • 1
  • 12
  • 25
0

Linq is not necessarily faster: it's a myth.

Linq use a lot of methods calls that take huge cpu cycles...

Certainly Linq is optimized and can make prousesses in a few calls that can sometimes be faster than code even written by an insider.

But depending on the type of objects you're acting on, Linq requires a lot of resources and will sometimes be slower.

The question to ask with Linq is above all about the quality of the code, its brevity, its robustness and its maintainability, and here it is a marvel.

For your code, you don't need Linq and it will not go faster.

You can add a break at the end of the if scope, so it stops the parsing after found the user:

foreach (var user in elite.Users)
  if ( user.UserName == txtuser.Text && user.UserPWD == txtpass.Text )
  {                    
      MainForm mainForm = new MainForm();
      mainForm.ShowDialog();
      Hide();
      Close();
      break;
  }

Using Linq, you can write:

if ( elite.Users.Any(user => user.UserName == txtuser.Text 
                          && user.UserPWD == txtpass.Text) )
{
  MainForm mainForm = new MainForm();
  mainForm.ShowDialog();
  Hide();
  Close();
}

As you see, you have no gain here with the code itself and it won't execute faster, nor slower, on a such simple thing.

Because, what does Linq ? It does a foreach somehow...

https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,e73922753675387a,references

https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,8087366974af11d2