Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am constructing a LINQ query on a DataSet, it will have a .Where condition, .Distinct and .OrderBy. I was thinking of doing this manually at first, at best I can probably manage this in two loops. Which method is faster? Is there anyway to see what LINQ does in the background?
Is there anyway to see what LINQ does in the background?
Well, you could look at my Edulinq implementation and the accompanying blog posts to get an idea of what's going on in LINQ to Objects. Obviously it's not the real implementation, but it will give you a good enough idea.
I would strongly suggest setting yourself some reasonable performance targets, implementing the code in the simplest possible way (which is almost certainly to use LINQ) and then seeing whether it meets your targets.
There are situations where hand-coding this sort of thing can bring significant improvements - but they're relatively rare, and you should only go for that after proving that it's worth it, IMO. LINQ is at least "pretty good" when used properly.
LINQ isn't about performance, it's about productivity. Building your own loops may result in a faster program, but unless you measure it, you wont know how much faster if at all.
On the other hand, you could probably write a Linq query in a fraction of the time spent writing the loops, and use the rest of that time optimizing the parts of the application that are slow and can be significantly improved.
Which is faster: me looking through a textbook page by page until I find the usages of a word I want, versus me asking someone else to look through the same textbook page by page looking for the usages, nudging me every time they do?
If you are concerned about performance, what is usually needed is a design change to accommodate that - for example, using the index.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I bet this question has been asked before, but I can't really find out what i'm looking for, so excuse me in advance :)
is there a difference (programmatically speaking OR overhead speaking) between this:
var data = GetProducts();
GetAllData(data);
and this:
GetAllData(GetProducts());
what are the pros and cons of both methods if any? is there a more elegant/right way of achieving it (say Func<>)?
thanks in advance,
Rotem
Doing it in two lines makes it easier to debug, because you can break on the second line and observe the value assigned on the first line.
The compiler will optimize them both into the same CIL anyway, so it's not a matter of efficiency. It's all a matter of preference.
There is no functional difference and when the code is translated into machine language (or JVM byte code or whatever), it will result in more or less the same low level code.
The main difference is a matter of (a) aesthetics and (b) maintainability of the code. With respect to aesthetics, some may argue that the second form is prettier. It's largely personal choice but I would argue that if the expression wasn't as simple as GetProducts() but was very long (e.g. GetContext().GetProductService().GetProductsFor(GetContext().GetCurrentUser()) then breaking it up into two lines with an intermediate variable is more readable.
With respect to maintainability, I think you will find that having fewer variables is always better for future maintenance. You are less likely to encounter bugs relating to side effects or changing assumptions. In other languages you can use constructs like const or final to use the compiler to help protect against code rot, but I would still argue that it's cleaner to have fewer lines of code.
Hope this helps!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a c# method that runs certain code blocks in a certain order. I want to pass a boolean parameter : "IsRunningInReverse" that will reverse the order in which the code blocks are run?
I could just create 2 private methods that just call the statements in normal and reverse order but I'm wondering if there is a better way to accomplish this?
I was thinking about creating delegates (Action<>) and storing them in a List and then the boolean "IsRunningInReverse" would determine whether I'd run through the list in ascending or descending order but I don't know if that's the cleanest solution.
Any input would be greatly appreciated.
Thanks!
Your solution seems fine to me.
Using Actions would be an idiomatic modern C# object wrapper for delegates, you can manipulate them easily.
To put them into a list is simple and will allow for what you want (reverse order), and even more (arbitrarily reorder them)
Maybe what might be more complicated and deserves some consideration is : would you need shared data ?
It should not be too difficult to handle this, but you would need a bit more structure (design some data sharing class for instance.
If you want to go further, once you feel more at ease with your solution, is maybe learn about expression trees .
That would be a powerful tool to manipulate different operations / actions. Beware, though, there is a large learning gap, it is quite more complicated than a List of Actions. If you simple want to reverse operations, I would still deem your List<Action> idea much more clean because it is much more simple and readable. I just thought it was worth mentioning.
How I would do it:
Make your "steps" separate functions.
Make a dictionary of the tasks and the "rank".
Then just sort asc/desc depending on what you are after.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
can you please tell me what is better to use?
for-each loop
create new object of class.
In mine case both aspect gave me same output.
so, my question is that what will i prefer to use.
If i choose 1st aspect.
whenever my page load at that time in fore-each loop(50 record fixed) will come.
and every time that loop execute so, my page execution is bit slower.
If i choose 2nd aspect
whenever my page load at that time new object is created and memory initialize
In asp.net mvc any inbuilt function or method to remove garbage collection(collection of unusual object) ?
please tell me what would i prefer for better use with relevant reason.
Write whichever one is simpler to code and more obviously correct. Then, see if your page is too slow or uses too much memory. (You get to decide what "too slow" or "too much" means, it's your application!)
If it works, great! Your problem is solved, work on adding more value to your application in some other way.
If it is too slow or uses too much memory, then try the less simple approach.
Then -- and this is the important part -- measure the difference for your application. Then, and only then, can you know the right answer.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm still learning much about immutability and when to use such objects and have started incorporating more Lookups into my coding for the fact that I knew them to be immutable and, hence, often better to use than Dictionaries that could be changed by clients.
That being said, I know there has been a good deal of work on introducing ReadOnlyDictionaries into .Net and I'm a bit confused where each would be more useful than the other and what specifically makes them different. I've looked online, but can't seem to find any articles explaining this difference.
For example, I saw this question and figured a Lookup would be a more ideal solution, but am confused as to why it wouldn't be.
Can anyone point me in the right direction / explain where either would be used over the other.
Thanks!!
Lookups are an easy way to group collections, where you could have one or more values for a given key. A Dictionary gives you one value for a give key, and one value only. Depending on your scenario, it may make most sense to have a Dictionary where you get back one value, or you may want to have a Lookup which would give you a collection of values.
Without a Lookup if you wanted a collection of values for a certain key you'd be stuck with something ugly like
Dictionary<int, IEnumerable<String>>
Yuk. See https://msdn.microsoft.com/en-us/library/bb460184%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 for more on Lookups.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In C#, how expensive it is to create a new class instance?
I'm speaking in context of using C# in unity3d. Meaning that stuff continuously gets called many times per second.
In C++, generally speaking (while making games) you may want to reuse anything you created with new/smart pointers, you would want to keep allocated resizeable buffers/lists/fifos and you may want to avoid that uses dynamic memory allocation (and stick to local variables) if code is getting performance critical.
So, what is the recommended way to do it in C#? Is it a very bad idea to create a new List, return it from the function and then "forget" about it, never using it again?
P.S. I'm aware of profiling and "premature optimizations", but I'd like to know some generic guidelines for the language before I (possibly) make a big mess because I used the wrong approach.
I remember reading a fun post Performance numbers in the pub by Ayendy Rahien.
How many CLR objects can you create in one second?
And here was the result back in 2011
Created 7,715,305 in 00:00:01
Jokes aside. Create is pretty cheap operation but GC is not. So while you can create really many objects, it is the collect that will hurt performance. So a rule of objects reuse can apply to C# as well.
I'd assume, but can be wrong, implementation of a new operator is located in JIT
aloc.h
aloc.cpp
As usual, avoid premature optimisation till you need it.