public void GenericsTypeSafety() //Generics types allow code reuse with type safety and Performance benefits { var dovesList = new List<Dove>(); //A List<T> is a Type that has one implementation but can work with any Object var firstDove = new Dove(); var secondDove = new Dove(); dovesList.Add(firstDove); dovesList.Add(secondDove); // dovesList.Add("StringDove"); This fails at compile time since the List<Dove> can only accept Doves Assert.AreEqual(true, dovesList.Any()); }
public void GetShortestRoute_should_consider_routes_in_order_from_small_distance() { using (new IndirectionsContext()) { // Arrange var slot = 0; var numAndDistances = new[] { 4, 2, 4, 3, 1, 6, 7 }; PRandom.NextInt32().Body = (@this, maxValue) => numAndDistances[slot++]; var vil = new Village(); var considerations = new List<RicePaddy>(); PList<RicePaddy>.AddT().Body = (@this, item) => { IndirectionsContext.ExecuteOriginal(() => { considerations.Add(item); @this.Add(item); }); }; // Act var result = vil.GetShortestRoute(vil.RicePaddies.ElementAt(2), vil.RicePaddies.ElementAt(0)); // Assert Assert.AreEqual(3, result.TotalDistance); Assert.AreEqual(4, considerations.Count); Assert.AreEqual(2, considerations[0].Identifier); Assert.AreEqual(1, considerations[1].Identifier); Assert.AreEqual(0, considerations[2].Identifier); Assert.AreEqual(3, considerations[3].Identifier); } }
public void TestValueTypePerformace() { using (new OperationTimer("List<int>")) { var l = new List<int>(Count); for (int i = 0; i < Count; i++) { l.Add(i); int x = l[i]; } l = null; } using (new OperationTimer("ArrayList of In32")) { var a = new ArrayList(Count); for (int i = 0; i < Count; i++) { a.Add(i); int x = (int)a[i]; } a = null; } }
public void TestReferenceTypePerformance() { using (new OperationTimer("List<string>")) { var l = new List<string>(Count); for (int i = 0; i < Count; i++) { l.Add("X"); string x = l[i]; } l = null; } using (new OperationTimer("ArrayList of String")) { var a = new ArrayList(Count); for (int i = 0; i < Count; i++) { a.Add("X"); string x = (string)a[i]; } a = null; } }
static void Main(string[] args) { MyCollection<int> intCollection = new MyCollection<int>(2, 6); intCollection.Add(3); for(int i=0;i<intCollection.Count();i++) Console.WriteLine(intCollection[i]); intCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < intCollection.Count(); i++) Console.WriteLine(intCollection[i]); int newInt = intCollection.CreateEntity(); Console.WriteLine("создали число "+newInt); //коллекция котов MyCollection<Cat> catCollection = new MyCollection<Cat>(); catCollection.Add(new Cat { Name = "Мурка", Weight = 8 }); catCollection.Add(new Cat { Name = "Мурзик", Weight = 15 }); catCollection.Add(new Cat { Name = "Барсик", Weight = 10 }); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); catCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); foreach (Cat cat in catCollection) Console.WriteLine(cat); Cat newCat = catCollection.CreateEntity(); Console.WriteLine("Создали кота "+newCat); DateTime time1 = DateTime.Now; ArrayList arrList = new ArrayList(); for (int i = 0; i < 1000000;i++ ) { arrList.Add(i);//упаковка int x =(int) arrList[i];//распаковка } Console.WriteLine((DateTime.Now-time1).TotalSeconds); arrList.Add("Вася"); DateTime time2 = DateTime.Now; List<int> list = new List<int>(); for (int i = 0; i < 1000000; i++) { list.Add(i); int x = list[i]; } //list.Add("Вася"); Console.WriteLine((DateTime.Now - time2).TotalSeconds); Console.ReadKey(); }
// We will get the shortest path between the begin RicePaddy and the end RicePaddy by Dijkstra's algorithm. Dictionary<RicePaddy, Route> CalculateShortestRoutes(RicePaddy start) { var shortestRoutes = new Dictionary<RicePaddy, Route>(); var handled = new List<RicePaddy>(); foreach (var ricePaddy in m_ricePaddies) { shortestRoutes.Add(ricePaddy, new Route(ricePaddy.Identifier)); } shortestRoutes[start].TotalDistance = 0; while (handled.Count != m_ricePaddies.Count) { var shortestRicePaddies = shortestRoutes.OrderBy(_ => _.Value.TotalDistance).Select(_ => _.Key).ToArray(); var processing = default(RicePaddy); foreach (var ricePaddy in shortestRicePaddies) { if (!handled.Contains(ricePaddy)) { if (shortestRoutes[ricePaddy].TotalDistance == int.MaxValue) return shortestRoutes; processing = ricePaddy; break; } } var selectedRoads = m_roads.Where(_ => _.A == processing); foreach (var road in selectedRoads) { if (shortestRoutes[road.B].TotalDistance > road.Distance + shortestRoutes[road.A].TotalDistance) { var roads = shortestRoutes[road.A].Roads.ToList(); roads.Add(road); shortestRoutes[road.B].Roads = roads; shortestRoutes[road.B].TotalDistance = road.Distance + shortestRoutes[road.A].TotalDistance; } } handled.Add(processing); } return shortestRoutes; }
public void GenericsPerformanceGains() //Generics perform so much better than normal collections { const int iterations = 10000000; var stopwatch = new Stopwatch(); stopwatch.Start(); var list = new ArrayList(); for (var currentIteration = 0; currentIteration <= iterations; currentIteration++) { list.Add(currentIteration); } foreach (var value in list.Cast<int>()) //We need to perform a cast in every member of the list { } stopwatch.Stop(); var arrayListMilliseconds = stopwatch.ElapsedMilliseconds; stopwatch.Reset(); stopwatch.Start(); var intList = new List<int>(); for (var currentIteration = 0; currentIteration <= iterations; currentIteration++) { intList.Add(currentIteration); //No cast is required } foreach (var integer in intList) { var value = integer; } stopwatch.Stop(); var listMilliseconds = stopwatch.ElapsedMilliseconds; //List<T> will always outperform ArrayList, CLR performs internal optimizations as well Assert.IsTrue(arrayListMilliseconds > listMilliseconds); }
private static List<string> GetNamesOfCodeFilesInProjectDirectory() { string path = Directory.GetCurrentDirectory(); string[] files = Directory.GetFiles(path + @"\..\..", "*.cs"); var fileNames = new List<string>(); foreach (var s in files) { FileInfo fileInfo = new FileInfo(s); if (!fileInfo.Name.Equals("Program.cs")) fileNames.Add(fileInfo.Name.Substring(0, fileInfo.Name.Length - 3)); } fileNames.Sort(); return fileNames; }