コード例 #1
0
        public void SpeedTest()
        {
            const string text       = "hello";
            const int    iterations = 100;

            GlobalEvents.AddListener <DummyMessage>(DummyMessageReciever);

            DummyMessage message = new DummyMessage(text);


            TimeSpan[] timeSpans = new TimeSpan[iterations];
            Stopwatch  stopwatch = null;

            for (int i = 0; i < iterations; i++)
            {
                stopwatch = Stopwatch.StartNew();
                for (int y = 0; y < 100000; y++)
                {
                    GlobalEvents.SendMessage(message);
                }
                stopwatch.Stop();
                timeSpans[i] = stopwatch.Elapsed;
            }
            GlobalEvents.RemoveListener <DummyMessage>(DummyMessageReciever);
            Assert.Pass($"MessageSystem Speedtest: {timeSpans.Average().Milliseconds}ms");
        }
コード例 #2
0
    static void Main()
    {
        const int iterations         = Benchmark.IterationCount,
                  warming_iterations = Benchmark.WarmingIterationCount;

        WakeUp();

        Console.WriteLine($">>> Warming with {warming_iterations} iterations...");

        var b       = new Benchmark();
        var timings = new TimeSpan[iterations];

        for (var i = 0; i < warming_iterations; i++)
        {
            b.runIteration();
        }

        b.reset();

        Console.WriteLine($">>> Running {iterations} iterations...");

        var sw = new Stopwatch();

        for (int i = 0; i < iterations; i++)
        {
            sw.Restart();
            b.Step();
            timings[i] = sw.Elapsed;
        }

        Console.WriteLine($">>> Elapsed {timings.Sum (t => t.TotalMilliseconds)}ms");
        Console.WriteLine($">>> ms/iter avg = {timings.Average (t => t.TotalMilliseconds)}");
        Console.WriteLine($">>> min = {timings.Min (t => t.TotalMilliseconds)}");
        Console.WriteLine($">>> max = {timings.Max (t => t.TotalMilliseconds)}");

        if (Debugger.IsAttached)
        {
            Console.WriteLine(">>> Press enter to exit.");
            Console.ReadLine();
        }
    }
コード例 #3
0
    private TResult RunInternal <TResult>(ulong count, Delegate func, params object[] args)
    {
        TimeSpan[] times = new TimeSpan[count];

        TResult result = default(TResult);

        for (ulong i = 0; i < count; ++i)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            result = (TResult)func.DynamicInvoke(args);

            watch.Stop();

            times[i] = watch.Elapsed;
        }

        Array.Sort(times);
        long     averageTicks = Convert.ToInt64(times.Average(timeSpan => timeSpan.Ticks));
        TimeSpan average      = new TimeSpan(averageTicks);

        Average = average.TotalSeconds;

        ulong middle = count / 2;

        if ((count & 1) == 1)
        {
            Mean = times[middle].TotalSeconds;
        }
        else
        {
            Mean = new TimeSpan((times[middle - 1].Ticks + times[middle].Ticks) / 2).TotalSeconds;
        }

        Min = times[0].TotalSeconds;
        Max = times[count - 1].TotalSeconds;

        return(result);
    }
コード例 #4
0
        public void Compare(int[] array)
        {
            Stopwatch stopWatch = new Stopwatch();

            TimeSpan[] timeSpan = new TimeSpan[1000];

            for (int i = 0; i < 1000; i++)
            {
                stopWatch.Reset();
                stopWatch.Start();
                searcher.Search(array);
                stopWatch.Stop();

                timeSpan[i] = stopWatch.Elapsed;
            }
            double   doubleAverageTicks = timeSpan.Average(ts => ts.Ticks);
            long     longAverageTicks   = Convert.ToInt64(doubleAverageTicks);
            TimeSpan d = new TimeSpan(longAverageTicks);

            timeSpanCollection.Add("Simple Method", d);



            for (int i = 0; i < 1000; i++)
            {
                stopWatch.Reset();
                stopWatch.Start();
                searcher.LinQSearch(array);
                stopWatch.Stop();

                timeSpan[i] = stopWatch.Elapsed;
            }
            double   doubleAverageTicks1 = timeSpan.Average(ts => ts.Ticks);
            long     longAverageTicks1   = Convert.ToInt64(doubleAverageTicks1);
            TimeSpan d1 = new TimeSpan(longAverageTicks1);

            timeSpanCollection.Add("LinQ", d1);


            for (int i = 0; i < 1000; i++)
            {
                stopWatch.Reset();
                stopWatch.Start();
                searcher.Search(array, new SearchWithCondition(searcher.SearchCondition));
                stopWatch.Stop();

                timeSpan[i] = stopWatch.Elapsed;
            }
            double   doubleAverageTicks2 = timeSpan.Average(ts => ts.Ticks);
            long     longAverageTicks2   = Convert.ToInt64(doubleAverageTicks2);
            TimeSpan d2 = new TimeSpan(longAverageTicks2);

            timeSpanCollection.Add("New Delegate", d2);

            for (int i = 0; i < 1000; i++)
            {
                stopWatch.Reset();
                stopWatch.Start();
                searcher.Search(array, searcher.search);
                stopWatch.Stop();

                timeSpan[i] = stopWatch.Elapsed;
            }
            double   doubleAverageTicks3 = timeSpan.Average(ts => ts.Ticks);
            long     longAverageTicks3   = Convert.ToInt64(doubleAverageTicks3);
            TimeSpan d3 = new TimeSpan(longAverageTicks3);

            timeSpanCollection.Add("Lambda", d3);

            for (int i = 0; i < 1000; i++)
            {
                stopWatch.Reset();
                stopWatch.Start();
                searcher.Search(array, searcher.anonymousSearch);
                stopWatch.Stop();

                timeSpan[i] = stopWatch.Elapsed;
            }
            double   doubleAverageTicks4 = timeSpan.Average(ts => ts.Ticks);
            long     longAverageTicks4   = Convert.ToInt64(doubleAverageTicks4);
            TimeSpan d4 = new TimeSpan(longAverageTicks4);

            timeSpanCollection.Add("Anonymous Method", d4);

            foreach (var element in timeSpanCollection)
            {
                Console.WriteLine($"Name of Method: {element.Key}. Avarage time: {element.Value}");
            }

            TimeSpan[] ta  = { d, d1, d2, d3, d4 };
            var        min = ta.Min();

            Console.WriteLine($"Minimum search time: {min}");
        }
コード例 #5
0
ファイル: Form1.cs プロジェクト: PretiSekar/FileAnalysis
        private void OpenFolder(object sender, EventArgs e)
        {
            //open the file explorer to choose the file on click
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Select a text file";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            //customize to view only text files
            openFileDialog1.DefaultExt       = "txt";
            openFileDialog1.Filter           = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex      = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }

            String filename = textBox1.Text;

            toolStripStatusLabel1.Text = "";
            String    line;
            int       no_count        = 0;
            int       backspace_count = 0;
            ArrayList starttime       = new ArrayList();
            ArrayList endtime         = new ArrayList();

            //read the existing file to read the data
            toolStripStatusLabel1.Text = "File Created";
            if (filename.Contains("CS6326"))
            {
                if (File.Exists(filename))
                {
                    using (StreamReader sw = new StreamReader(filename))
                    {
                        while ((line = sw.ReadLine()) != null)
                        {
                            char[]   delimiters = new char[] { '\t' };
                            string[] words      = line.Split(delimiters);
                            starttime.Add(words[13]);
                            endtime.Add(words[14]);
                            no_count++;
                            backspace_count = backspace_count + Convert.ToInt32(words[15]);
                        }
                    }
                }



                //get the start time
                DateTime[] st = new DateTime[starttime.Count];
                for (int i = 0; i < st.Length; i++)
                {
                    st[i] = Convert.ToDateTime(starttime[i]);
                }

                //get the end time
                DateTime[] et = new DateTime[endtime.Count];
                for (int i = 0; i < et.Length; i++)
                {
                    et[i] = Convert.ToDateTime(endtime[i]);
                }

                //timediff
                TimeSpan[] entry = new TimeSpan[st.Length];
                for (int i = 0; i < st.Length; i++)
                {
                    entry[i] = (st[i] - et[i]);
                    //entry[i] = ts.ToString(@"hh\:mm\:ss");
                }
                //calculating minimum entry time

                TimeSpan min_entry = entry[0];
                for (int i = 0; i < entry.Length; i++)
                {
                    if (TimeSpan.Compare(entry[i], min_entry) > 0)
                    {
                        min_entry = entry[i];
                    }
                }

                //calculating maximum entry time

                TimeSpan max_entry = entry[0];
                for (int i = 0; i < entry.Length; i++)
                {
                    if (TimeSpan.Compare(entry[i], max_entry) < 0)
                    {
                        max_entry = entry[i];
                    }
                }

                //calculating average entry time

                double   doubleAverage = entry.Average(timeSpan => timeSpan.Ticks);
                long     longAverage   = Convert.ToInt64(doubleAverage);
                TimeSpan avg_entry     = new TimeSpan(Math.Abs(longAverage));



                TimeSpan[] inter = new TimeSpan[st.Length - 1];
                for (int i = 0; i < st.Length - 1; i++)
                {
                    inter[i] = st[i + 1] - et[i];
                    //entry[i] = ts.ToString(@"hh\:mm\:ss");
                }
                //calculating minimum entry time

                TimeSpan min_inter = inter[0];
                for (int i = 0; i < inter.Length; i++)
                {
                    if (TimeSpan.Compare(inter[i], min_inter) > 0)
                    {
                        min_inter = inter[i];
                    }
                }

                //calculating maximum entry time

                TimeSpan max_inter = inter[0];
                for (int i = 0; i < inter.Length; i++)
                {
                    if (TimeSpan.Compare(inter[i], max_inter) < 0)
                    {
                        max_inter = inter[i];
                    }
                }


                //calculating average entry time
                double doubleAve = inter.Average(timeSpan => timeSpan.Ticks);
                long   longAve   = Convert.ToInt64(doubleAve);

                TimeSpan avg_inter = new TimeSpan(longAve);
                //calculate total
                TimeSpan temp1 = new TimeSpan();
                TimeSpan temp2 = new TimeSpan();
                TimeSpan sum   = new TimeSpan();
                for (int i = 0; i < inter.Length; i++)
                {
                    temp1 = temp1.Add(inter[i]);
                }
                for (int i = 0; i < entry.Length; i++)
                {
                    temp1 = temp1.Add(entry[i]);
                }
                sum = temp1.Add(temp2).Negate();
                //create a new file and input all the entries
                String file = @"Asg3_Analysis.txt";
                using (StreamWriter tw = File.CreateText(file))
                {
                    tw.Write("Number of records:" + "\t");
                    tw.Write(no_count + "\n");
                    tw.Write("Minimum entry time:" + "\t");
                    tw.Write(min_entry.ToString(@"hh\:mm\:ss") + "\n");
                    tw.Write("Maximum entry time:" + "\t");
                    tw.Write(max_entry.ToString(@"hh\:mm\:ss") + "\n");
                    tw.Write("Average entry time:" + "\t");
                    tw.Write(avg_entry + "\n");
                    tw.Write("Minimum inter-record time:" + "\t");
                    tw.Write(min_inter + "\n");
                    tw.Write("Maximum inter-record time:" + "\t");
                    tw.Write(max_inter + "\n");
                    tw.Write("Average inter-record time:" + "\t");
                    tw.Write(avg_inter + "\n");
                    tw.Write("Total time:" + "\t");
                    tw.Write(sum + "\n");
                    tw.Write("Backspace count:" + "\t");
                    tw.Write(backspace_count + "\n");
                    tw.Close();
                    toolStripStatusLabel1.Text = "File Analyzed and available for view as Asg3_Analysis.txt";
                }
            }
            else
            {
                toolStripStatusLabel1.Text = "Select a valid file for analysis";
            }
        }
コード例 #6
0
        public static async Task SelectiveUpdate(Clan clan, Changes changes, List <DiscordMessage> modifyMessages, Action <List <DiscordMessage>, TimeSpan, Changes> modifyMethod)
        {
            await Read(clan);

            int size = (int)MathF.Ceiling((float)changes.addedUsers.Count / 5);

            TimeSpan[] durations   = new TimeSpan[size];
            var        workingList = clan.members.BungieUsers;
            int        index       = 0;

            foreach (User addedUser in changes.addedUsers)
            {
                var startTime   = DateTime.UtcNow;
                var workingUser = new User();
                if (!string.IsNullOrEmpty(addedUser.BungieProfile))
                {
                    var a = await BungieTools.GetBungieUserID(addedUser.BungieProfile);

                    workingUser.BungieID = a.ToString();
                    if (!string.IsNullOrEmpty(workingUser.BungieID))
                    {
                        string bungieProfile             = addedUser.BungieProfile;
                        string bungieID                  = workingUser.BungieID;
                        UserStatus.StatusEnum userStatus = addedUser.UserStatus;
                        GeneralUser           bungieUser = await BungieTools.GetBungieUser(Convert.ToInt64(bungieID));

                        string steamID      = SteamTools.GetSteamID(bungieProfile);
                        string steamProfile = "https://steamcommunity.com/profiles/" + steamID;
                        string bungieName   = bungieUser.displayName;
                        string steamName    = SteamTools.GetSteamUserName(steamID);
                        string discordID    = addedUser.DiscordID;
                        string userClanTag  = addedUser.ClanTag;
                        workingUser = new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, userClanTag);
                        workingList.Add(workingUser);
                    }
                    else
                    {
                        List <string> extraColumns  = new List <string>();
                        string        bungieProfile = addedUser.BungieProfile;
                        string        bungieID      = "N/A";
                        string        steamID       = "N/A";
                        string        steamProfile  = "N/A";
                        string        bungieName    = "N/A";
                        string        steamName     = "N/A";
                        string        discordID     = addedUser.DiscordID;
                        if (addedUser.BungieID != "N/A" || addedUser.BungieID != "")
                        {
                            bungieID = addedUser.BungieID;
                        }
                        if (addedUser.SteamID != "N/A" || addedUser.SteamID != "")
                        {
                            steamID = addedUser.SteamID;
                        }
                        if (addedUser.SteamProfile != "N/A" || addedUser.SteamProfile != "")
                        {
                            steamProfile = addedUser.SteamProfile;
                        }
                        if (addedUser.BungieName != "N/A" || addedUser.BungieName != "")
                        {
                            bungieName = addedUser.BungieName;
                        }
                        if (addedUser.SteamName != "N/A" || addedUser.SteamName != "")
                        {
                            steamName = addedUser.SteamName;
                        }

                        if (addedUser.ExtraColumns != null)
                        {
                            extraColumns = addedUser.ExtraColumns;
                        }
                        UserStatus.StatusEnum userStatus = addedUser.UserStatus;

                        if (addedUser.ExtraColumns != null)
                        {
                            extraColumns = addedUser.ExtraColumns;
                        }
                        workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns));
                    }
                }
                durations[index % durations.Length] = DateTime.UtcNow - startTime;

                if (index > durations.Length - 1)
                {
                    var averageTime = TimeSpan.FromMilliseconds(durations.Average(t => t.TotalMilliseconds));
                    var usersLeft   = changes.addedUsers.Count - (index + 1);
                    if (modifyMessages != null && modifyMethod != null)
                    {
                        modifyMethod.Invoke(modifyMessages, averageTime * usersLeft, changes);
                    }
                }
                index++;
            }
            for (int i = 0; i < changes.updatedUsers.Count; i++)
            {
                index = workingList.FindIndex(t => t.BungieProfile == changes.updatedUsers[i].BungieProfile);
                workingList[index] = changes.updatedUsers[i];
            }
            changes.removedUsers.ForEach(removedUser =>
            {
                index = workingList.FindIndex(t => t.BungieProfile == removedUser.BungieProfile);
                workingList.RemoveAt(index);
            });
            workingList.RemoveAll(t => string.IsNullOrEmpty(t.BungieProfile));
            workingList = workingList.DistinctBy(t => t.BungieProfile).ToList();
            workingList = workingList.OrderBy(t => t.SteamName).ToList();
            clan.members.BungieUsers = workingList;
            Write(clan);
            Clans.SaveClanMembers(clan);
        }
コード例 #7
0
        public static async Task BulkUpdate(Clan clan, List <DiscordMessage> modifyMessages, Action <List <DiscordMessage>, TimeSpan> modifyMethod, bool skipRead = false)
        {
            TimeSpan[] durations = new TimeSpan[5];
            if (!skipRead)
            {
                await Read(clan);
            }
            //ShowLoading("processing...");
            var         _           = clan.members.SpreadsheetUsers;
            List <User> workingList = new List <User>();
            int         index       = 0;

            foreach (User user in _)
            {
                var startTime   = DateTime.UtcNow;
                var workingUser = new User();
                if (!string.IsNullOrEmpty(user.BungieProfile))
                {
                    long?a = await BungieTools.GetBungieUserID(user.BungieProfile);

                    workingUser.BungieID = a.ToString();
                    if (!string.IsNullOrEmpty(workingUser.BungieID))
                    {
                        List <string> extraColumns  = new List <string>();
                        string        bungieProfile = user.BungieProfile;
                        string        bungieID      = workingUser.BungieID;
                        GeneralUser   bungieUser    = await BungieTools.GetBungieUser(Convert.ToInt64(bungieID));

                        string steamID      = SteamTools.GetSteamID(bungieProfile);
                        string steamName    = SteamTools.GetSteamUserName(steamID);
                        string steamProfile = "https://steamcommunity.com/profiles/" + steamID;
                        string bungieName   = bungieUser.displayName;
                        string discordID    = user.DiscordID;
                        UserStatus.StatusEnum userStatus = user.UserStatus;

                        if (user.ExtraColumns != null)
                        {
                            extraColumns = user.ExtraColumns;
                        }
                        if (steamName == null || steamID == null)
                        {
                            steamName = "N/A"; steamProfile = "N/A";
                        }
                        workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns));
                    }
                    else
                    {
                        List <string>         extraColumns  = new List <string>();
                        string                bungieProfile = user.BungieProfile;
                        string                bungieID      = "N/A";
                        string                steamID       = "N/A";
                        string                steamProfile  = "N/A";
                        string                bungieName    = "N/A";
                        string                steamName     = "N/A";
                        string                discordID     = user.DiscordID;
                        UserStatus.StatusEnum userStatus    = user.UserStatus;
                        if (user.BungieID != "N/A" || user.BungieID != null)
                        {
                            bungieID = user.BungieID;
                        }
                        if (user.SteamID != "N/A" || user.SteamID != null)
                        {
                            steamID = user.SteamID;
                        }
                        if (user.SteamProfile != "N/A" || user.SteamProfile != null)
                        {
                            steamProfile = user.SteamProfile;
                        }
                        if (user.BungieName != "N/A" || user.BungieName != null)
                        {
                            bungieName = user.BungieName;
                        }
                        if (user.SteamName != "N/A" || user.SteamName != null)
                        {
                            steamName = user.SteamName;
                        }

                        if (user.ExtraColumns != null)
                        {
                            extraColumns = user.ExtraColumns;
                        }
                        workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns));
                    }
                }
                durations[index % durations.Length] = DateTime.UtcNow - startTime;

                if (index > durations.Length - 1)
                {
                    var averageTime = TimeSpan.FromMilliseconds(durations.Average(t => t.TotalMilliseconds));
                    var usersLeft   = _.Count - (index + 1);
                    if (modifyMessages != null && modifyMethod != null)
                    {
                        modifyMethod.Invoke(modifyMessages, averageTime * usersLeft);
                    }
                }
                index++;
            }
            workingList.RemoveAll(t => string.IsNullOrEmpty(t.BungieProfile));
            workingList = workingList.DistinctBy(t => t.BungieProfile).ToList();
            workingList = workingList.OrderBy(t => t.SteamName).ToList();
            clan.members.BungieUsers = workingList;
            Write(clan);
            Clans.SaveClanMembers(clan);
        }