views:

48

answers:

1

I have a result set that will never exceed 500; the results that come back from the web-service are assigned to a search results object.

The data from the webservice is about 2mb; the bit I want to use is about a third of each record, so this allows me to cache and quickly manipulate it.

I want to be able to sort and filter the results with the least amount of overhead and as fast as possible so I used the VCSKICKS timing class to measure their performance

              Average        Total (10,000)
Type       Create  Sort      Create  Sort
HashSet    0.1579  0.0003    1579    3
IList      0.0633  0.0002    633     2
IQueryable 0.0072  0.0432    72      432

Measured in Seconds using http://www.vcskicks.com/algorithm-performance.php

I created the hashset through a for loop over the web-service response (adding to the hashset).

The List & IQueryable were created using LINQ.

Question I can understand why HashSet takes longer to create (the foreach loop vs linq); but why would IQueryable take longer to sort than the other two; and finally is there a better way to assign the HashSet.

Thanks

Actual Program

public class Program
{
    private static AuthenticationHeader _authHeader;
    private static OPSoapClient _opSession;
    private static AccommodationSearchResponse _searchResults;

    private static HashSet<SearchResults> _myHash;
    private static IList<SearchResults> _myList;
    private static IQueryable<SearchResults> _myIQuery;

    static void Main(string[] args)
    {
        #region Setup WebService
        _authHeader = new AuthenticationHeader { UserName = "xx", Password = "xx" };
        _opSession = new OPSoapClient();


        #region Setup Search Results

        _searchResults = _opgSession.SearchCR(_authHeader, "ENG", "GBP", "GBR");

        #endregion Setup Search Results

        #endregion Setup WebService


        // HASHSET
        SpeedTester hashTest = new SpeedTester(TestHashSet);
        hashTest.RunTest();
        Console.WriteLine("- Hash Test \nAverage Running Time: {0}; Total Time: {1}", 
            hashTest.AverageRunningTime, 
            hashTest.TotalRunningTime);

        SpeedTester hashSortTest = new SpeedTester(TestSortingHashSet);
        hashSortTest.RunTest();
        Console.WriteLine("- Hash Sort Test \nAverage Running Time: {0}; Total Time: {1}",
            hashSortTest.AverageRunningTime,
            hashSortTest.TotalRunningTime);

        // ILIST
        SpeedTester listTest = new SpeedTester(TestList);
        listTest.RunTest();
        Console.WriteLine("- List Test \nAverage Running Time: {0}; Total Time: {1}",
            listTest.AverageRunningTime,
            listTest.TotalRunningTime);

        SpeedTester listSortTest = new SpeedTester(TestSortingList);
        listSortTest.RunTest();
        Console.WriteLine("- List Sort Test \nAverage Running Time: {0}; Total Time: {1}",
            listSortTest.AverageRunningTime,
            listSortTest.TotalRunningTime);

        // IQUERIABLE
        SpeedTester iqueryTest = new SpeedTester(TestIQueriable);
        iqueryTest.RunTest();
        Console.WriteLine("- iquery Test \nAverage Running Time: {0}; Total Time: {1}",
            iqueryTest.AverageRunningTime,
            iqueryTest.TotalRunningTime);

        SpeedTester iquerySortTest = new SpeedTester(TestSortableIQueriable);
        iquerySortTest.RunTest();
        Console.WriteLine("- iquery Sort Test \nAverage Running Time: {0}; Total Time: {1}",
            iquerySortTest.AverageRunningTime,
            iquerySortTest.TotalRunningTime);
    }

    static void TestHashSet()
    {
        var test = _searchResults.Items;

        _myHash = new HashSet<SearchResults>();

        foreach(var x in test)
        {
            _myHash.Add(new SearchResults
                                     {
                                         Ref = x.Ref,
                                         Price = x.StandardPrice
                                     });
        }

    }

    static void TestSortingHashSet()
    {
        var sorted = _myHash.OrderBy(s => s.Price);
    }



    static void TestList()
    {
        var test = _searchResults.Items;

        _myList = (from x in test
                   select new SearchResults
                              {
                                  Ref = x.Ref,
                                  Price = x.StandardPrice
                              }).ToList();
    }

    static void TestSortingList()
    {
        var sorted = _myList.OrderBy(s => s.Price);
    }

    static void TestIQueriable()
    {
        var test = _searchResults.Items;

        _myIQuery = (from x in test
                     select new SearchResults
                                {
                                    Ref = x.Ref,
                                    Price = x.StandardPrice
                                }).AsQueryable();

    }

    static void TestSortableIQueriable()
    {
        var sorted = _myIQuery.OrderBy(s => s.Price);
    }

}

=== Direct Output from console ===

- Hash Test
Average Running Time: 0.154; Total Time: 1540
- Hash Sort Test
Average Running Time: 0.0003; Total Time: 3
- List Test
Average Running Time: 0.0601; Total Time: 601
- List Sort Test
Average Running Time: 0.0002; Total Time: 2
- iquery Test
Average Running Time: 0.0003; Total Time: 3
- iquery Sort Test
Average Running Time: 0.0399; Total Time: 399
- IEnum Test
Average Running Time: 0.0002; Total Time: 2
- IEnum Sort Test
Average Running Time: 0.0001; Total Time: 1
Press any key to continue . . .

Obviously times vary based on what my machine was doing at the time, you can usually make some judgement based on the size of the difference but as the 'answer' states its unfair to judge IQueryable the same way.

+2  A: 

I think IQueryable has deferred the creation until the sort was executed where the IList did the creation up front and then performed the sort on it.

Just like the IList, the HashSet creation was not deferred and therefore the the big up front cost.

So up front the HashSet and the IList have to be created which takes time which you record. Then you record the sorting on them which is probably negligible. The IQueryable deferred that creation time UNTIL you did the sort so the create and sort are all happening in the time for the sort. The sort didn't really take longer for the IQueryable it just looks like it because the creation is happening at the same time in your testing. I would suspect that all 3 sort's time differences are negligible.

I think either way it is fine to use any of them depending on how you want the output to be used or extended in the case of IQueryable.

Kelsey
Thanks the MSDN descriptions not great. http://msdn.microsoft.com/en-us/library/bb397727.aspx
Chris M