Choosing a type for search results in C#
Posted
by Chris M
on Stack Overflow
See other posts from Stack Overflow
or by Chris M
Published on 2010-05-25T22:31:45Z
Indexed on
2010/05/25
22:51 UTC
Read the original article
Hit count: 296
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);
}
}
© Stack Overflow or respective owner