Having seen the question about SO, about joining strings, I did some testing and realized that joining a string in foreach is slower than with a for loop and using indexes in an array. Should the for loop run slower due to related array validation? (linked string checking [i], which is absent in foreach).
Another thing I don't understand is string.Join () slowness in lists ...
EDIT: updated the error and updated the source file to the final source (deleting the last ",")
Here is the test result:
DEBUG:
AMD PHENOM II X4 3GHZ
StringBuilder foreach System.Action Time: 4077ms (12025926)
StringBuilder for System.Action Time: 4032ms (11895082)
String.Join System.Action Time: 5338ms (15744918)
INTEL XEON W3503 @ 2.4GHZ / 12GB DDR3
StringBuilder foreach System.Action Time: 4661ms (10926950)
StringBuilder for System.Action Time: 4202ms (9849590)
String.Join System.Action Time: 6466ms (15156149)
RELEASE:
AMD PHENOM II X4 3GHZ
StringBuilder foreach System.Action Time: 3897ms (11496978)
StringBuilder for System.Action Time: 3719ms (10970899)
String.Join System.Action Time: 5307ms (15655162)
INTEL XEON W3503 @ 2.4GHZ / 12GB DDR3
StringBuilder foreach System.Action Time: 4533ms (10625128)
StringBuilder for System.Action Time: 4168ms (9770765)
String.Join System.Action Time: 7173ms (16813036)
(why in the world xeon slower than in debug with string.join?)
FOR A GOOD LAUGH LOOK AT THE END.
And here is the source:
public static void Main(string[] Args)
{
List<string> strings = new List<string>() {};
for (double d = 0; d < 12000; d++) {
strings.Add(d.ToString());
}
GC.Collect();
GC.WaitForPendingFinalizers();
Performance(() =>
{
StringBuilder sb = new StringBuilder();
foreach (string s in strings)
{
sb.Append(s);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
}, "StringBuilder foreach");
GC.Collect();
GC.WaitForPendingFinalizers();
Performance(() =>
{
StringBuilder sb = new StringBuilder();
int max = strings.Count-1;
int i;
for (i = 0; i < max; i++)
{
sb.Append(strings[i]);
sb.Append(",");
}
sb.Append(strings[i]);
}, "StringBuilder for");
GC.Collect();
GC.WaitForPendingFinalizers();
Performance(() =>
{
string s = string.Join(",", strings);
}, "String.Join");
}
public static void Performance(Action fn, string prefix)
{
var timer = new Stopwatch();
timer.Start();
for (var i = 0; i < 10000; ++i)
{
fn();
}
timer.Stop();
Console.WriteLine("{0} {1} Time: {2}ms ({3})", prefix, fn.ToString(), timer.ElapsedMilliseconds, timer.ElapsedTicks);
}
Are strings copied as values of type foreach? Since the speed is almost the same ...
EDIT:
, int max = strings.Count-1; , ( ):
, , . string.Length for, ( , ).. , , ( get) 5% . , "max". , .
EDIT2:
, , , String.Join():
List<string> strings = new List<string>() {};
for (double d = 0; d < 12000; d++) {
strings.Add("ikugluglizuglkuhiugpiugiugholiugholiughpiuhziuhzuiugloiu" + d.ToString());
}
, String.Join , !
, :
Windows 7 64bit
CPU Type QuadCore AMD Phenom II X4 945
CPU Clock 3000 MHz
L3 Cache 6 MB (On-Die, ECC, NB-Speed)
North Bridge Clock 2010.8 MHz
Memory 8190 MB
Memory Bus 804.3 MHz DDR3-1600
Motherboard Chipset AMD 790X, AMD K10
Memory Timings 8-9-9-24 (CL-RCD-RP-RAS)
Command Rate (CR) 1T