C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: We use no string literals, which would be interned, placed in the executable.
String LiteralInfo: The more chars that must be compared, the longer the comparison will take. A program should use the shortest string data it can.
C# program that tests string performance
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
string a1 = new string('a', 2);
string a2 = new string('a', 2);
string b1 = new string('a', 6);
string b2 = new string('a', 6);
string c1 = new string('a', 12);
string c2 = new string('a', 12);
int counter = 0;
const int m = 100000000;
// Version 1: compare 2 char strings.
Stopwatch s1 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (a1 == a2)
{
counter++;
}
}
s1.Stop();
// Version 2: compare 6-char strings.
Stopwatch s2 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (b1 == b2)
{
counter++;
}
}
s2.Stop();
// Version 3: compare 12-char strings.
Stopwatch s3 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (c1 == c2)
{
counter++;
}
}
s3.Stop();
Console.WriteLine("2 CHAR COMPARE: " + s1.ElapsedMilliseconds);
Console.WriteLine("6 CHAR COMPARE: " + s2.ElapsedMilliseconds);
Console.WriteLine("12 CHAR COMPARE: " + s3.ElapsedMilliseconds);
Console.Read();
}
}
Output
2 CHAR COMPARE: 478
6 CHAR COMPARE: 551
12 CHAR COMPARE: 588
Note: Each string object required a constant 16 bytes for the object data. And the buffer requires 2 bytes per character.
Therefore: The memory usage estimate for a string instance is 16 + (2 * Length) bytes.
Simulation: We run a simulation where a varying number of strings of a certain length are allocated and placed in an array.
And: The length of the array is changed to ensure the numbers remain constant for each individual string of a specific length.
String ArrayC# program that measures memory usage of strings
using System;
class Program
{
static void Main()
{
// Loop through these sizes of string sets.
// ... These are array lengths of string references.
int[] sizes = { 0, 1, 100, 1000, 10000 };
foreach (int size in sizes)
{
// Allocate the array of references.
string[] array = new string[size];
long b1 = GC.GetTotalMemory(true);
// Each string is a fixed length.
for (int i = 0; i < array.Length; i++)
{
array[i] = new string('a', 10);
}
long b2 = GC.GetTotalMemory(true);
// Ensure the array is not optimized out of the program.
if (array.Length > 0)
{
array[0] = null;
}
// Write out statistics.
Console.WriteLine("Count: {0} Memory: {1} Chars: {2}",
size,
b2 - b1,
10 * size);
}
Console.ReadLine();
}
}
Output
Count: 0 Memory: 0 Chars: 0
Count: 1 Memory: 36 Chars: 10
Count: 100 Memory: 3600 Chars: 1000
Count: 1000 Memory: 36000 Chars: 10000
Count: 10000 Memory: 360000 Chars: 100000
Tip: You can sometimes use StringBuilder to transform many strings into a single string, allowing for fewer objects on the managed heap.
So: Strings use less memory, which means your programs will require fewer garbage collections.