static void Main(string[] args) { string source = "abc def ghi"; string left = StringUtils.Left(source, 3); string right = StringUtils.Right(source, 3); Console.WriteLine(left); Console.WriteLine(right); // Performance-Test Console.WriteLine(); Console.WriteLine("Performance-Test"); HighResStopClock sc = new HighResStopClock(); //StreamWriter sr = new StreamWriter("c:\\performance.txt"); //Console.SetOut(sr); string name; // Die Methoden einmal aufrufen, sodass die CLR diese kompiliert StringUtils.Left(source, 3); StringUtils.Right(source, 3); name = "Left"; sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.Left(source, 3); } Console.WriteLine("{0}: {1}", name, sc.Stop()); name = "Substring(0, count)"; sc.Start(); for (int i = 0; i < 10000; i++) { source.Substring(0, 3); } Console.WriteLine("{0}: {1}", name, sc.Stop()); name = "Right"; sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.Right(source, 3); } Console.WriteLine("{0}: {1}", name, sc.Stop()); name = "Substring(Length - count, count)"; sc.Start(); for (int i = 0; i < 10000; i++) { source.Substring(source.Length - 3, 3); } Console.WriteLine("{0}: {1}", name, sc.Stop()); // sr.Close(); Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }
static void Main(string[] args) { string source = "Das ist ein Test-String mit 11 Wörtern\r\nund einem Zeilenumbruch."; long wordCount = StringUtils.WordCount2(source); Console.WriteLine("Der String\r\n'{0}'\r\nhat {1} Wörter.", source, wordCount); source = "Das ist ein Unicode-Test-String mit 13 Wörtern und speziellen Zeichen ÿÖÜø"; wordCount = StringUtils.WordCount2(source); Console.WriteLine("Der String\r\n'{0}'\r\nhat {1} Wörter.", source, wordCount); // Performance-Test Console.WriteLine("Performance-Test"); HighResStopClock sc = new HighResStopClock(); source = "ab cd ef"; sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.WordCount1(source); } Console.WriteLine("WordCount1: {0}", sc.Stop()); sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.WordCount1(source); } Console.WriteLine("WordCount1: {0}", sc.Stop()); sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.WordCount2(source); } Console.WriteLine("WordCount2: {0}", sc.Stop()); sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.WordCount2(source); } Console.WriteLine("WordCount2: {0}", sc.Stop()); Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }
private void sortSubNodes_Click(object sender, System.EventArgs e) { // Alle Knoten der ersten Ebene durchgehen und deren // Unterknoten sortieren HighResStopClock sc = new HighResStopClock(); sc.Start(); for (int i = 0; i < bookTree.Nodes.Count; i++) { ControlUtils.SortTreeViewNodes(bookTree.Nodes[i].Nodes); } infoLabel.Text = sc.Stop().ToString() + " Sekunden"; }
static void Main(string[] args) { // Zeit ungenau (mit einem Intervall von etwa 15 bis 55ms) über die Ticks // der DateTime-Struktur messen Console.WriteLine("Zeitmessung über DateTime.Now.Ticks:"); long startTicks = DateTime.Now.Ticks; int i = 0; while (i < 1000000) { i++; } long ticks = (DateTime.Now.Ticks - startTicks); double seconds = ticks / 10000000F; Console.WriteLine("Benötigte Zeit: {0} Ticks, {1} Sekunden", ticks, seconds); // Zeit genau über QueryPerformanceCounter messen Console.WriteLine(); Console.WriteLine("Zeitmessung über QueryPerformanceCounter:"); HighResStopClock stopClock; try { stopClock = new HighResStopClock(); Console.WriteLine("Aktuelle Frequenz: {0}", stopClock.PCFrequency); Console.WriteLine("Aktuelle Auflösung: {0:G} Sekunden", stopClock.PCResolution); stopClock.Start(); i = 0; while (i < 1000000) { i++; } seconds = stopClock.Stop(); Console.WriteLine("Benötigte Zeit: {0} Sekunden", seconds); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }
static void Main(string[] args) { HighResStopClock sc = new HighResStopClock(); string source = "ab cd ab cd ab cd ab cd"; #if WRITETOFILE StreamWriter Console = new StreamWriter("C:\\String-Replace-Performance.txt"); #endif // string.Replace sc.Start(); for (int i = 0; i < 10000; i++) { source.Replace("ab", "XYZ"); } Console.WriteLine("string.Replace: {0}", sc.Stop()); // StringBuilder.Replace sc.Start(); for (int i = 0; i < 10000; i++) { StringBuilder sb = new StringBuilder(source); sb.Replace("ab", "XYZ"); } Console.WriteLine("StringBuilder.Replace: {0}", sc.Stop()); // Regex.Replace mit binärem Vergleich sc.Start(); Regex re = new Regex("ab"); int count = -1; for (int i = 0; i < 10000; i++) { re.Replace(source, "XYZ", count, 0); } Console.WriteLine("Regulärer Ausdruck (Binär): {0}", sc.Stop()); // Regex.Replace mit Textvergleich sc.Start(); re = new Regex("ab", RegexOptions.IgnoreCase); count = -1; for (int i = 0; i < 10000; i++) { re.Replace(source, "XYZ", count, 0); } Console.WriteLine("Regulärer Ausdruck (Text): {0}", sc.Stop()); // Microsoft.VisualBasic.Strings.Replace - binärer Vergleich sc.Start(); for (int i = 0; i < 10000; i++) { Microsoft.VisualBasic.Strings.Replace(source, "ab", "XYZ", 1, -1, CompareMethod.Binary); } Console.WriteLine("VB-Replace (Binary): {0}", sc.Stop()); // Microsoft.VisualBasic.Strings.Replace - Textvergleich sc.Start(); for (int i = 0; i < 10000; i++) { Microsoft.VisualBasic.Strings.Replace(source, "ab", "XYZ", 1, -1, CompareMethod.Text); } Console.WriteLine("VB-Replace (Binär): {0}", sc.Stop()); // Eigene Replace-Methode Microsoft - binärer Vergleich sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.Replace(source, "ab", "XYZ", false, 1, -1); } Console.WriteLine("Replace (Binär): {0}", sc.Stop()); // Eigene Replace-Methode - Textvergleich sc.Start(); for (int i = 0; i < 10000; i++) { StringUtils.Replace(source, "ab", "XYZ", true, 1, -1); } Console.WriteLine("Replace (Text): {0}", sc.Stop()); #if WRITETOFILE Console.Close(); #else Console.WriteLine("Beenden mit Return"); Console.ReadLine(); #endif }
public static void Main(string[] args) { HighResStopClock sc = new HighResStopClock(); //StreamWriter sr = new StreamWriter("C:\\Performance.txt"); Console.WriteLine("Performance-Messung ..."); string name; StringCollection colDummy = new StringCollection(); colDummy.Add("a"); colDummy.Add("b"); colDummy.Add("c"); // Drei Duchläufe StringCollection col1 = null; StringCollection col2 = null; StringCollection col3 = null; double time1 = 0, time2 = 0, time3 = 0; for (int i = 0; i < 3; i++) { col1 = new StringCollection(); col2 = new StringCollection(); col3 = new StringCollection(); System.GC.WaitForPendingFinalizers(); for (int j = 0; j < 1000; j++) { string s = StringUtils.RandomString(50); col1.Add(s); col2.Add(s); col3.Add(s); } SortUtils.Quicksort(colDummy); SortUtils.Heapsort(colDummy); SortUtils.Sort(colDummy); name = "Quicksort"; sc.Start(); SortUtils.Quicksort(col1); double seconds = sc.Stop(); time1 += seconds; Console.WriteLine("{0}: {1}", name, seconds); //sr.WriteLine("{0}: {1}", name, seconds); name = "Heapsort"; sc.Start(); SortUtils.Heapsort(col2); seconds = sc.Stop(); time2 += seconds; Console.WriteLine("{0}: {1}", name, seconds); //sr.WriteLine("{0}: {1}", name, seconds); name = "Sort"; sc.Start(); SortUtils.Sort(col3); seconds = sc.Stop(); time3 += seconds; Console.WriteLine("{0}: {1}", name, seconds); //sr.WriteLine("{0}: {1}", name, seconds); } Console.WriteLine("Quicksort (mittel): {0}", time1 / 3); //sr.WriteLine("Quicksort (mittel): {0}", time1 / 3); Console.WriteLine("Heapsort (mittel): {0}", time2 / 3); //sr.WriteLine("Heapsort (mittel): {0}", time2 / 3); Console.WriteLine("Sort (mittel): {0}", time3 / 3); //sr.WriteLine("Sort (mittel): {0}", time3 / 3); //sr.Close(); //Process.Start("notepad.exe", "c:\\Performance.txt"); //for (int i = 0; i < col3.Count; i++) // Console.WriteLine(col3[i]); Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }
static void Main(string[] args) { HighResStopClock sc = new HighResStopClock(); string logFileName = Path.Combine(Application.StartupPath, "Log.txt"); StreamWriter sw = new StreamWriter(logFileName, false, Encoding.Default); PerformanceCounter pc = new PerformanceCounter("Process", "Working Set"); pc.InstanceName = Process.GetCurrentProcess().ProcessName; Console.WriteLine("Start-Speicherauslastung: {0:0} KB\r\n", pc.NextValue() / 1024); sw.WriteLine("Start-Speicherauslastung: {0:0} KB\r\n", pc.NextValue() / 1024); FileInfo fi = new FileInfo( Path.Combine(Application.StartupPath, "Hitchhiker.txt")); Console.WriteLine("Datei-Größe: {0} Byte\r\n", fi.Length); sw.WriteLine("Datei-Größe: {0} Byte\r\n", fi.Length); double sumSeconds1 = 0; double sumSeconds2 = 0; int testCycles = 10000; for (int i = 0; i < testCycles; i++) { SymmetricEncryptor se = null; try { se = new SymmetricEncryptor(SymmetricEncryptAlgorithm.Rijndael); se.Key = "abcdefgh12345678äöüÄÖÜßßø£Ø×*áíó"; se.IV = "46¶0?-B,-7,kerkh"; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Beenden mit Return"); Console.ReadLine(); return; } // Eine Datei verschlüsseln FileStream sourceStream = new FileStream( Path.Combine(Application.StartupPath, "Hitchhiker.txt"), FileMode.Open, FileAccess.Read); FileStream destStream = new FileStream( Path.Combine(Application.StartupPath, "Hitchhiker_Encrypted.txt"), FileMode.Create, FileAccess.Write); sc.Start(); se.Encrypt(sourceStream, destStream); double seconds1 = sc.Stop(); sumSeconds1 += seconds1; sourceStream.Close(); destStream.Close(); // Eine Datei entschlüsseln sourceStream = new FileStream( Path.Combine(Application.StartupPath, "Hitchhiker_Encrypted.txt"), FileMode.Open, FileAccess.Read); destStream = new FileStream( Path.Combine(Application.StartupPath, "Hitchhiker_Decrypted.txt"), FileMode.Create, FileAccess.Write); sc.Start(); se.Encrypt(sourceStream, destStream); double seconds2 = sc.Stop(); sumSeconds2 += seconds2; sourceStream.Close(); destStream.Close(); sw.WriteLine("Durchlauf {0}", i); sw.WriteLine("Verschlüsseln: {0}", seconds1); sw.WriteLine("Entschlüsseln: {0}", seconds2); sw.WriteLine("Speicherauslastung: {0:0} KB", pc.NextValue() / 1024); sw.WriteLine(); Console.WriteLine("Durchlauf {0}", i); Console.WriteLine("Verschlüsseln: {0}", seconds1); Console.WriteLine("Entschlüsseln: {0}", seconds2); Console.WriteLine("Speicherauslastung: {0:0} KB", pc.NextValue() / 1024); Console.WriteLine(); System.GC.WaitForPendingFinalizers(); } Console.WriteLine("\r\nDurchschnittliche Zeit für das Verschlüsseln: {0}", sumSeconds1 / testCycles); sw.WriteLine("\r\nDurchschnittliche Zeit für das Verschlüsseln: {0}", sumSeconds1 / testCycles); Console.WriteLine("\r\nDurchschnittliche Zeit für das Entschlüsseln: {0}", sumSeconds2 / testCycles); sw.WriteLine("\r\nDurchschnittliche Zeit für das Entschlüsseln: {0}", sumSeconds2 / testCycles); Console.WriteLine("\r\nEnd-Speicherauslastung: {0:0} KB", pc.NextValue() / 1024); sw.WriteLine("\r\nEnd-Speicherauslastung: {0:0} KB", pc.NextValue() / 1024); sw.Close(); Process.Start("notepad.exe", logFileName); Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }