Click here to Skip to main content
15,877,384 members
Home / Discussions / C#
   

C#

 
GeneralRe: Building Objects with Reflection Pin
Dennis Klein12-Nov-03 3:25
Dennis Klein12-Nov-03 3:25 
Generalcommunicate with service Pin
Member 67650610-Nov-03 22:45
Member 67650610-Nov-03 22:45 
GeneralRe: communicate with service Pin
Arjan Einbu10-Nov-03 23:03
Arjan Einbu10-Nov-03 23:03 
GeneralCOM+ application load balancing Pin
CillyMe10-Nov-03 20:34
CillyMe10-Nov-03 20:34 
GeneralLoading data to string variable Pin
Rostrox10-Nov-03 19:19
Rostrox10-Nov-03 19:19 
GeneralRe: Loading data to string variable Pin
Corinna John10-Nov-03 19:47
Corinna John10-Nov-03 19:47 
GeneralRe: Loading data to string variable Pin
Jeff Varszegi11-Nov-03 4:54
professionalJeff Varszegi11-Nov-03 4:54 
GeneralRe: Loading data to string variable Pin
Jeff Varszegi11-Nov-03 6:04
professionalJeff Varszegi11-Nov-03 6:04 
I was interested enough to run a performance test because I never used AppendFormat before, and I know that StringBuilder is highly optimized. I ran this code in Debug mode in VS .NET 2003 on my laptop:

C#
StringBuilder sb;
string text = "abc";
int x, y;
long startTime, endTime;
 <br>
startTime = DateTime.Now.Ticks;
for(y = 0; y < 5000; y++) {
    sb = new StringBuilder(10000);
    for (x = 0; x < 250; x++) {   
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
        sb.Append(text).Append('\n');
    }
}
endTime = DateTime.Now.Ticks;
Console.WriteLine(((endTime - startTime) / 10000) + " ms using Append()");
 <br>
startTime = DateTime.Now.Ticks;
for(y = 0; y < 5000; y++) {
    sb = new StringBuilder(10000);
    for (x = 0; x < 250; x++) {
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
        sb.AppendFormat("{0}\n", text);
    }
}
endTime = DateTime.Now.Ticks;
Console.WriteLine(((endTime - startTime) / 10000) + " ms using AppendFormat()");

It printed these results:
<font color="#000066">1682 ms using Append()</font>
<font color="#000066">7801 ms using AppendFormat()</font>

Interestingly enough, creating a separate variable to hold the format string and using that in calls to AppendFormat increased the run time of the second section a little. I still know next to nothing about the IL, but I'm sure there's some good explanation for this.

Regards,

Jeff Varszegi
GeneralControl/Class Existence Pin
Anonymous10-Nov-03 17:47
Anonymous10-Nov-03 17:47 
GeneralRe: Control/Class Existence Pin
Heath Stewart11-Nov-03 3:36
protectorHeath Stewart11-Nov-03 3:36 
GeneralRe: Control/Class Existence Pin
Bhangorix11-Nov-03 23:55
Bhangorix11-Nov-03 23:55 
GeneralRe: Control/Class Existence Pin
Heath Stewart12-Nov-03 2:57
protectorHeath Stewart12-Nov-03 2:57 
GeneralRe: Control/Class Existence Pin
Bhangorix16-Nov-03 19:12
Bhangorix16-Nov-03 19:12 
GeneralConverting an Image to an Icon Pin
Adam Turner10-Nov-03 13:47
Adam Turner10-Nov-03 13:47 
GeneralRe: Converting an Image to an Icon Pin
Corinna John10-Nov-03 19:54
Corinna John10-Nov-03 19:54 
GeneralNew Line in CF textbox Pin
gadgetfbi10-Nov-03 13:01
gadgetfbi10-Nov-03 13:01 
GeneralRe: New Line in CF textbox Pin
MultiThread10-Nov-03 16:54
MultiThread10-Nov-03 16:54 
QuestionHow to retrieve the initial window state? Pin
Alvaro Mendez10-Nov-03 12:20
Alvaro Mendez10-Nov-03 12:20 
AnswerRe: How to retrieve the initial window state? Pin
Heath Stewart11-Nov-03 3:38
protectorHeath Stewart11-Nov-03 3:38 
GeneralRe: How to retrieve the initial window state? Pin
Alvaro Mendez11-Nov-03 4:33
Alvaro Mendez11-Nov-03 4:33 
GeneralRe: How to retrieve the initial window state? Pin
Heath Stewart11-Nov-03 6:54
protectorHeath Stewart11-Nov-03 6:54 
GeneralRe: How to retrieve the initial window state? Pin
Alvaro Mendez11-Nov-03 8:48
Alvaro Mendez11-Nov-03 8:48 
GeneralRe: How to retrieve the initial window state? Pin
Heath Stewart11-Nov-03 8:53
protectorHeath Stewart11-Nov-03 8:53 
GeneralComputer name Pin
Chathura10-Nov-03 12:10
Chathura10-Nov-03 12:10 
GeneralRe: Computer name Pin
Heath Stewart11-Nov-03 3:43
protectorHeath Stewart11-Nov-03 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.