Skip to content
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

Replace LINQ's .Any() with foreach in Encode #25

Merged
merged 1 commit into from
Sep 14, 2023
Merged

Conversation

aradalvand
Copy link
Collaborator

@aradalvand
Copy link
Collaborator Author

aradalvand commented Sep 13, 2023

@LeaFrock Does this look good?
I chose foreach instead of for because I think a foreach on an array gets lowered to a for by the compiler, if I'm not mistaken. But feel free to correct me.

@LeaFrock
Copy link
Contributor

a foreach on an array gets lowered to a for by the compiler

No 😄 . for is always the fastest. But for an array/list, the compiler will make foreach the same as for.

Originally, I just wanted to replace Any with Array.Exists. for or foreach requires more code lines after all. The key point is that Any has extra memory allocation. The CPU-perf gap between them can be ignored as it's too small compared to the Encode algorithm.

Since you've adopted foreach, it's perfect anyway.

@aradalvand aradalvand merged commit ab87dad into main Sep 14, 2023
2 checks passed
@aradalvand aradalvand deleted the linq-to-loop branch September 15, 2023 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants