What among the below three approaches is the best?
1. using + operator
2. using string.concat
3. using stringbuilder
I understand that best means the fastest. I was unable to answer so I wrote
simple .Net application, look at the results.
private const string _content = @"Query notifications were introduced in SQL
Server 2005 and SQL Server Native Client. Built upon the Service Broker
infrastructure introduced in SQL Server 2005, query notifications allow
applications to be notified when data has changed. This feature is
particularly useful for applications that provide a cache of information
from a database, such as a Web application, and need to be notified when the
source data is changed.
Query notifications allow you to request notification within a specified
time-out period when the underlying data of a query changes. The request for
notification specifies the notification options, which include the service
name, message text, and time-out value to the server. Notifications are
delivered through a Service Broker queue that applications may poll for
available notifications.
The syntax of the query notifications options string is:
service=<service-name>[;(local database=<database> | broker instance=<broker
instance>)]
For example:
service=mySSBService;local database=mydb
Notification subscriptions outlive the process that initiates them, as an
application may create a notification subscription and then terminate. The
subscription remains valid, and the notification will occur if the data
changes within the time-out period specified when the subscription was
created. A notification is identified by the query executed, the
notification options, and the message text, and may be cancelled by setting
its time-out value to zero.
Notifications are sent only once. For continuous notification of data
change, a new subscription must be created by re-executing the query after
each notification is processed.
SQL Server Native Client applications typically receive notifications by
using the Transact-SQL RECEIVE command to read notifications from the queue
associated with the service specified in the notification options.";
private const int _numOfIterations = 10000000;
static void Main(string[] args)
{
TestConcatenation();
}
private static void TestConcatenation()
{
string strA, strB, strC, result;
TimeSpan strCreationTime, plusOperatorTime, strConcatTime,
strBuilderTime;
DateTime startTime;
StringBuilder sb;
strCreationTime = plusOperatorTime = strConcatTime = strBuilderTime =
TimeSpan.Zero;
for (int i = 0; i < _numOfIterations; i++)
{
startTime = DateTime.Now;
strA = GetRandomString(DateTime.Now.Millisecond);
strB = GetRandomString(DateTime.Now.Millisecond);
strC = GetRandomString(DateTime.Now.Millisecond);
strCreationTime += DateTime.Now - startTime;
startTime = DateTime.Now;
result = strA + strB + strC;
plusOperatorTime += DateTime.Now - startTime;
startTime = DateTime.Now;
result = string.Concat(strA, strB, strC);
strConcatTime += DateTime.Now - startTime;
startTime = DateTime.Now;
sb = new StringBuilder();
sb.AppendFormat("{0}{1}{2}", strA, strB, strC);
result = sb.ToString();
strBuilderTime += DateTime.Now - startTime;
}
//print stats
sb = new StringBuilder();
sb.AppendFormat("String creation time: {0}{1}",
strCreationTime.ToString(), Environment.NewLine);
sb.AppendFormat("Plus operator time: {0}{1}",
plusOperatorTime.ToString(), Environment.NewLine);
sb.AppendFormat("String concat time: {0}{1}", strConcatTime.ToString(),
Environment.NewLine);
sb.AppendFormat("String builder time: {0}{1}",
strBuilderTime.ToString(), Environment.NewLine);
Console.Write(sb.ToString());
}
private static string GetRandomString(int seed)
{
Random rnd;
int strStartInd, strLength;
rnd = new Random(seed);
strStartInd = rnd.Next(0, _content.Length - 1);
strLength = rnd.Next(0, _content.Length - strStartInd);
return _content.Substring(strStartInd, strLength);
}
Results for 10 000 000 iterations:
/*
String creation time: 00:03:46.3054112
Plus operator time: 00:00:20.4093472
String concat time: 00:00:25.9272816
String builder time: 00:00:50.9232240
*/
Dawid Rutyna