Example #1
0
        public void compareContainerTest()
        {
            Console.WriteLine("Skaitomas failas...");

            List <Student>       baseList       = new List <Student>();
            Queue <Student>      baseQueue      = new Queue <Student>();
            LinkedList <Student> baseLinkedList = new LinkedList <Student>();

            Reader.ReadStudentsFromFile("_100000.txt", (Student s) => {
                s.getGradeAvg(false);

                baseList.Add(s);
                baseQueue.Enqueue(s);
                baseLinkedList.AddLast(s);
            });

            Console.WriteLine("Atliekami testai...");
            Profiler profiler = new Profiler();

            {
                List <Student> high = new List <Student>();
                List <Student> low  = new List <Student>();

                profiler.begin("List 1 strategija");
                baseList.ForEach((Student s) => { if (s.AverageGrade >= 5.0f)
                                                  {
                                                      high.Add(s);
                                                  }
                                                  else
                                                  {
                                                      low.Add(s);
                                                  } });
                profiler.end("List 1 strategija");

                high.Clear();
                low.Clear();
            }

            {
                List <Student> high = new List <Student>(baseList);
                List <Student> low  = new List <Student>();

                profiler.begin("List 2 strategija");

                for (int i = high.Count - 1; i >= 0; i--)
                {
                    if (high[i].AverageGrade < 5.0f)
                    {
                        low.Add(high[i]);
                        high.RemoveAt(i);
                    }
                }

                profiler.end("List 2 strategija");

                high.Clear();
                low.Clear();
            }


            {
                profiler.begin("List optimizuota strategija");
                int marker    = baseList.Count / 2;
                int direction = 0;

                baseList.Sort((o1, o2) => o1.AverageGrade.CompareTo(o2.AverageGrade));

                if (baseList.ElementAt(marker).AverageGrade >= 5.0f)
                {
                    direction = -1;
                }
                else
                {
                    direction = 1;
                }

                while (true)
                {
                    marker += direction;

                    if (direction > 0)
                    {
                        if (baseList.ElementAt(marker).AverageGrade >= 5.0f)
                        {
                            break;
                        }
                    }

                    if (direction < 0)
                    {
                        if (baseList.ElementAt(marker).AverageGrade < 5.0f)
                        {
                            marker++;
                            break;
                        }
                    }
                }

                profiler.end("List optimizuota strategija");
            }

            {
                Queue <Student> high = new Queue <Student>();
                Queue <Student> low  = new Queue <Student>();

                profiler.begin("Queue 1 strategija");
                foreach (Student s in baseQueue)
                {
                    if (s.AverageGrade >= 5.0f)
                    {
                        high.Enqueue(s);
                    }
                    else
                    {
                        low.Enqueue(s);
                    }
                }
                profiler.end("Queue 1 strategija");

                high.Clear();
                low.Clear();
            }

            {
                Queue <Student> high = new Queue <Student>();
                Queue <Student> low  = new Queue <Student>();

                profiler.begin("Queue 2 strategija");
                while (baseQueue.Count != 0)
                {
                    Student s = baseQueue.Dequeue();

                    if (s.AverageGrade >= 5.0f)
                    {
                        high.Enqueue(s);
                    }
                    else
                    {
                        low.Enqueue(s);
                    }
                }
                profiler.end("Queue 2 strategija");

                high.Clear();
                low.Clear();
            }

            {
                LinkedList <Student> high = new LinkedList <Student>();
                LinkedList <Student> low  = new LinkedList <Student>();

                profiler.begin("LinkedList 1 strategija");
                foreach (Student s in baseLinkedList)
                {
                    if (s.AverageGrade >= 5.0f)
                    {
                        high.AddLast(s);
                    }
                    else
                    {
                        low.AddLast(s);
                    }
                }
                profiler.end("LinkedList 1 strategija");

                high.Clear();
                low.Clear();
            }

            {
                LinkedList <Student> high = new LinkedList <Student>(baseLinkedList);
                LinkedList <Student> low  = new LinkedList <Student>();

                profiler.begin("LinkedList 2 strategija");
                for (int i = high.Count - 1; i >= 0; i--)
                {
                    if (high.ElementAt(i).AverageGrade < 5.0f)
                    {
                        low.AddLast(high.ElementAt(i));
                        high.Remove(high.ElementAt(i));
                    }
                }
                profiler.end("LinkedList 2 strategija");

                high.Clear();
                low.Clear();
            }

            profiler.process();

            baseList.Clear();
            baseQueue.Clear();
            baseLinkedList.Clear();
        }
Example #2
0
        private void generateStudentLists()
        {
            Console.Clear();

            Stopwatch w = new Stopwatch();

            w.Start();

            Random r = new Random();

            for (int i = 0; i < 7; i++)
            {
                List <Student> list = new List <Student>();

                for (int j = 0; j < Math.Pow(10, i + 1); j++)
                {
                    Student student = new Student();
                    student.Name      = "Student_" + j;
                    student.LastName  = "Sirname_" + j;
                    student.ExamGrade = r.Next(0, 11);

                    for (int k = 0; k < 10; k++)
                    {
                        student.AddHomeworkGrade(r.Next(0, 11));
                    }

                    student.getGradeAvg(false);

                    list.Add(student);
                }


                Writer.WriteStudentsToFile("_" + Math.Pow(10, i + 1) + ".txt", list);

                list.Sort((o1, o2) => o1.AverageGrade.CompareTo(o2.AverageGrade));
                int marker    = list.Count / 2;
                int direction = 0;

                if (list.ElementAt(marker).AverageGrade >= 5.0f)
                {
                    direction = -1;
                }
                else
                {
                    direction = 1;
                }

                while (true)
                {
                    marker += direction;

                    if (direction > 0)
                    {
                        if (list.ElementAt(marker).AverageGrade >= 5.0f)
                        {
                            break;
                        }
                    }

                    if (direction < 0)
                    {
                        if (list.ElementAt(marker).AverageGrade < 5.0f)
                        {
                            marker++;
                            break;
                        }
                    }
                }

                Writer.WriteStudentsToFile("_" + Math.Pow(10, i + 1) + "_vargsiukai.txt", list.GetRange(0, marker));
                Writer.WriteStudentsToFile("_" + Math.Pow(10, i + 1) + "_galvociai.txt", list.GetRange(marker, list.Count - marker));
            }

            w.Stop();
            Console.WriteLine("Ivykdyta per: " + w.ElapsedMilliseconds + "ms");

            Run();
        }