Click here to Skip to main content
15,897,273 members
Home / Discussions / C#
   

C#

 
AnswerRe: Executing delegate in a different thread Pin
Laddie21-May-08 4:30
Laddie21-May-08 4:30 
GeneralRe: Executing delegate in a different thread Pin
Artur Löwen21-May-08 4:58
Artur Löwen21-May-08 4:58 
GeneralRe: Executing delegate in a different thread Pin
Peter Josefsson Sweden21-May-08 5:33
Peter Josefsson Sweden21-May-08 5:33 
GeneralRe: Executing delegate in a different thread Pin
Bekjong21-May-08 5:38
Bekjong21-May-08 5:38 
GeneralRe: Executing delegate in a different thread Pin
Peter Josefsson Sweden21-May-08 13:00
Peter Josefsson Sweden21-May-08 13:00 
QuestionRead USB device names Pin
kristofvanderhaeghen21-May-08 3:35
kristofvanderhaeghen21-May-08 3:35 
AnswerRe: Read USB device names Pin
Giorgi Dalakishvili21-May-08 3:44
mentorGiorgi Dalakishvili21-May-08 3:44 
QuestionStringBuilder Speed Question Pin
Reelix21-May-08 3:11
Reelix21-May-08 3:11 
I've been playing around with the StringBuilder class, and found something confusing...

The code is as follows:

static void Main(string[] args)
        {
            Stopwatch sw1 = new Stopwatch();
            Console.WriteLine("Press enter to being the test.");
            Console.ReadLine();
            Console.WriteLine("Test 1 is addind 10 characters 1,000,000 times.");
            Console.WriteLine("Test 1 Starting");
            string smallWord = "1234567890";
            StringBuilder longWord = new StringBuilder();
            sw1.Start();
            for (int j = 0; j < 1000000; j++)
            {
                longWord.Append(smallWord);
            }
            sw1.Stop();
            Console.WriteLine("Test 1 Complete in " + sw1.ElapsedMilliseconds + "ms");
            Console.WriteLine("-");
            Console.WriteLine("Test 2 is removing 10 characters 500 times.");
            Console.WriteLine("Test 2 Starting (Will take several seconds)");
            sw1.Reset();
            sw1.Start();
            for (int k = 0; k < 500; k++)
            {
                longWord.Remove(0, 10);
            }
            sw1.Stop();
            Console.WriteLine("Test 2 Complete in " + sw1.ElapsedMilliseconds + "ms");
            Console.WriteLine("-");
            Console.WriteLine("Press Enter to Quit.");
            Console.ReadLine();
        }


My question:

How come it is EXTREMELY fast(91ms) to ADD 10,000,000 characters, yet takes SO long (+- 6 seconds) to remove a total of 5000 characters?

Is there anyway to optimise the above code to make it any faster?

Any help would be appreciated Smile | :)

-= Reelix =-

AnswerRe: StringBuilder Speed Question PinPopular
Guffa21-May-08 4:00
Guffa21-May-08 4:00 
QuestionRe: StringBuilder Speed Question Pin
buchstaben21-May-08 4:01
buchstaben21-May-08 4:01 
AnswerRe: StringBuilder Speed Question PinPopular
Colin Angus Mackay21-May-08 4:07
Colin Angus Mackay21-May-08 4:07 
AnswerRe: StringBuilder Speed Question Pin
Reelix21-May-08 4:25
Reelix21-May-08 4:25 
GeneralRe: StringBuilder Speed Question Pin
Mike Dimmick21-May-08 6:28
Mike Dimmick21-May-08 6:28 
Questionread sql messages Pin
Peterpod21-May-08 3:09
Peterpod21-May-08 3:09 
AnswerRe: read sql messages Pin
Peter Josefsson Sweden21-May-08 4:34
Peter Josefsson Sweden21-May-08 4:34 
QuestionHow to Cancel KeyDown event ? Pin
hdv21221-May-08 2:38
hdv21221-May-08 2:38 
AnswerRe: How to Cancel KeyDown event ? Pin
Giorgi Dalakishvili21-May-08 3:26
mentorGiorgi Dalakishvili21-May-08 3:26 
GeneralRe: How to Cancel KeyDown event ? Pin
hdv21221-May-08 3:37
hdv21221-May-08 3:37 
QuestionHow to get custom property name of docx file using IFilter? Pin
Prazwol21-May-08 1:43
Prazwol21-May-08 1:43 
QuestionEfficient image processing Pin
Jon Hulatt21-May-08 1:18
Jon Hulatt21-May-08 1:18 
AnswerRe: Efficient image processing Pin
Pete O'Hanlon21-May-08 2:26
mvePete O'Hanlon21-May-08 2:26 
QuestionHow to use custom control dll in other application Pin
Krish Agn21-May-08 1:06
Krish Agn21-May-08 1:06 
AnswerRe: How to use custom control dll in other application Pin
Christian Graus21-May-08 1:13
protectorChristian Graus21-May-08 1:13 
GeneralRe: How to use custom control dll in other application Pin
Krish Agn21-May-08 1:55
Krish Agn21-May-08 1:55 
GeneralRe: How to use custom control dll in other application Pin
Reelix21-May-08 3:16
Reelix21-May-08 3:16 

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.