History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: RSRP-61062
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Eugene Pasynkov
Reporter: Yuri Astrakhan
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
ReSharper

Suggest Enumerable* optimizations

Created: 11 Mar 08 20:33   Updated: 12 Mar 08 01:28
Component/s: Code Analysis
Fix Version/s: Alderman
Security Level: Everybody (All jira users)

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 748


 Description  « Hide
var arr = new[] {1, 2, 3};
var x = arr.Where(i => i==2).Single();
var y = arr.Single(i => i == 2);

Both x and y assignments are functionally identical because Single() can take a "where" func parameter, but if understand everything correctly, the second call is much more efficient. R# can suggest to make this improvement automatically.



 All   Comments   Work Log   Change History      Sort Order:
Yuri Astrakhan - 11 Mar 08 22:24
I did some sample runs on this code, trying to determine the impact of nested yields, and got about 2085 vs 1445 ms - about 44% improvement
private static void RunJoined(int runCount)
{
    int count = 0;
    DateTime start = DateTime.Now;
    for (int i = 0; i < runCount; i++)
        count += Numbers.Count(v => v > 100);
    DateTime end = DateTime.Now;
    Console.WriteLine("duration: {0} milliseconds ({1})", (end - start).TotalMilliseconds, count);
}

private static void RunNested(int runCount)
{
    int count = 0;
    DateTime start = DateTime.Now;
    for (int i = 0; i < runCount; i++)
        count += Numbers.Where(v => v > 100).Count();
    DateTime end = DateTime.Now;
    Console.WriteLine("duration: {0} milliseconds ({1})", (end - start).TotalMilliseconds, count);
}

Yuri Astrakhan - 11 Mar 08 22:25
Forgot for previous comment:
private static IEnumerable<int> Numbers
        {
            get
            {
                for (int i = 0; i < 10000; i++)
                    yield return i;
            }
        }