1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace ConcatenationChaine
{
public class Exemple
{
static readonly string[] NOMS = { "Dupont", "Durant", "Dupuis", "Dulac", "Dupré", "Delatour", "Desbois", "Dubois", "Deschamps" };
static readonly string[] PRENOMS = { "Alain", "Barbara", "Charlie", "Denis", "Eric", "Fabrice", "Gaston", "Hervé", "Ingrid", "Julien", "Kamel", "Laure", "Marie", "Nicolas", "Oscar", "Paul", "Quentin", "Ralph", "Steeve", "Tom", "Ursule", "Victor", "William", "Yves", "Zoé" };
public static void Run()
{
Random rnd = new Random();
long memory1 = 0;
long memory2 = 0;
long memory3 = 0;
long memory4 = 0;
long time1 = 0;
long time2 = 0;
long time3 = 0;
long time4 = 0;
Stopwatch sw = new Stopwatch();
Console.Write("Génération des gens...");
sw.Start();
List<Person> personnes = new List<Person>();
for (int i = 0; i < 100; ++i)
{
personnes.Add(new Person() { Nom = NOMS[rnd.Next(NOMS.Length)], Prenom = PRENOMS[rnd.Next(PRENOMS.Length)], Naissance = DateTime.Now.AddDays(-rnd.Next(1000, 36500)) });
}
sw.Stop();
Console.WriteLine($"{sw.ElapsedMilliseconds} ms");
for (int j = 1; j < 100; ++j)
{
//Console.WriteLine("Concaténation avec string.Format()...");
GC.Collect();
long memoryBefore = GC.GetTotalMemory(true);
sw.Restart();
string s;
for (int i = 0; i < 100; ++i)
{
foreach (Person p in personnes)
{
s = p.ToStringFormat();
}
}
sw.Stop();
memory1 += GC.GetTotalMemory(false) - memoryBefore;
time1 += sw.ElapsedMilliseconds;
//Console.WriteLine($"{sw.ElapsedMilliseconds} ms, {GC.GetTotalMemory(false) - memoryBefore} octets");
//Console.WriteLine("Concaténation avec $\"{}\"...");
GC.Collect();
memoryBefore = GC.GetTotalMemory(true);
sw.Restart();
for (int i = 0; i < 100; ++i)
{
foreach (Person p in personnes)
{
s = p.ToStringFormat2();
}
}
sw.Stop();
memory2 += GC.GetTotalMemory(false) - memoryBefore;
time2 += sw.ElapsedMilliseconds;
//Console.WriteLine($"{sw.ElapsedMilliseconds} ms, {GC.GetTotalMemory(false) - memoryBefore} octets");
//Console.WriteLine("Concaténation avec un StringBuilder...");
GC.Collect();
memoryBefore = GC.GetTotalMemory(true);
sw.Restart();
for (int i = 0; i < 100; ++i)
{
foreach (Person p in personnes)
{
s = p.ToStringStringBuilder();
}
}
sw.Stop();
memory3 += GC.GetTotalMemory(false) - memoryBefore;
time3 += sw.ElapsedMilliseconds;
//Console.WriteLine($"{sw.ElapsedMilliseconds} ms, {GC.GetTotalMemory(false) - memoryBefore} octets");
//Console.WriteLine("Concaténation avec des +...");
GC.Collect();
memoryBefore = GC.GetTotalMemory(true);
sw.Restart();
for (int i = 0; i < 300; ++i)
{
foreach (Person p in personnes)
{
s = p.ToStringPlus();
}
}
sw.Stop();
memory4 += GC.GetTotalMemory(false) - memoryBefore;
time4 += sw.ElapsedMilliseconds;
//Console.WriteLine($"{sw.ElapsedMilliseconds} ms, {GC.GetTotalMemory(false) - memoryBefore} octets");
}
Console.WriteLine($"string.Format() {time1 / 100} ms, {memory1 / 100} octets");
Console.WriteLine($"$\"{{}}\" {time2 / 100} ms, {memory2 / 100} octets");
Console.WriteLine($"StringBuilder {time3 / 100} ms, {memory3 / 100} octets");
Console.WriteLine($"+ {time4 / 100} ms, {memory4 / 100} octets");
}
}
class Person
{
public string Nom { get; set; }
public string Prenom { get; set; }
public DateTime Naissance { get; set; }
public int Age
{
get
{
int Years = DateTime.Now.Year - Naissance.Year;
if (DateTime.Now.DayOfYear < Naissance.DayOfYear)
{
Years--;
}
return Years;
}
}
public string ToStringPlus()
{
return Prenom.Substring(0, 1) + " " + Nom + " (" + Age + ")";
}
public string ToStringFormat()
{
return string.Format("{0} {1} ({2})", Prenom.Substring(0, 1), Nom, Age);
}
public string ToStringFormat2()
{
return $"{Prenom.Substring(0, 1)} {Nom} ({Age})";
}
public string ToStringStringBuilder()
{
StringBuilder sb = new StringBuilder();
sb.Append(Prenom.Substring(0, 1));
sb.Append(' ');
sb.Append(Nom);
sb.Append(" (");
sb.Append(Age);
sb.Append(')');
return sb.ToString();
}
}
} |
Partager