-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid the use of Linq #939
Comments
Now with the first entry as 1 int iterations = 100_000;
List<int> list = new List<int>(new int[1000]);
[TestMethod]
public void WithoutLinq()
{
list[0] = 1;
int yes = 0;
for (int x = 0; x < iterations; x++)
{
foreach(var y in list)
{
if (y == 1)
{
yes++;
break;
}
}
}
Assert.AreEqual(iterations, yes);
}
[TestMethod]
public void WithLinq()
{
list[0] = 1;
int yes = 0;
for (int x = 0; x < iterations; x++)
{
if (list.Any(y => y == 1))
{
yes++;
}
}
Assert.AreEqual(iterations, yes);
} Results in my computer:
|
Man, this is crazy.. is it Release code? |
The previous result was under Last entry:
First entry:
|
If you pass a Func<T, bool> to a method, shouldnt it be the same as Linq Any? Example: // helper class
bool MyAny(this Enumerable<T> set, Func<T, bool> f){
foreach(v in set)
if(f(v))
return true;
return false;
}
// test
if (list.MyAny(y => y == 1))
{
yes++;
} Could you try this please shargon? |
@shargon, I understand that this type of optimization is more important in certain parts, for example listing the addresses of a wallet is not as important as the consensus . Can we start by listing the code where this optimization would have more impact? |
Agree, i will work on this in a future, in certains parts are better the readability |
@shargon I would like to eliminate the low-priority tag, it does not help us. I would like to make prioritization 'per release' instead of per issue. |
Of course! |
@shargon have you ran some performance test on the code (I'm not criticizing, I'm asking cause I haven't)? I can remember that once I was talking with a colleague about the possibility of using if (a & 1 == 0) {
// even
}
if (a % 2 == 0) {
// even
} |
I really think that in a future optimization phase "Avoid Linq" is something to keep in mind, just for critical methods. Currently I haven't got time to do benchmarks and I created this issue based on my experience in the past, so if you are interested in do some benchmarks, just do it. But i think that this could be a low priority issue |
Before proceeding Igor's question: #939 (comment) and @rodoufu should be taken into account. |
@shargon Great work! Think radically, we may need to replace all linq statements, at least in terms of affecting TPS, we need to do this, such as all linq statements about transaction processing. |
I understand that Linq makes our life easier (I ❤️ Linq), but the speed cost is very high, i will put an example here of the difference with the same logic
Code:
Results in my computer:
We should try to avoid the use of Linq in order to get more TPS
The text was updated successfully, but these errors were encountered: