Click here to Skip to main content
15,910,009 members
Home / Discussions / C#
   

C#

 
AnswerRe: Object lifetime, GC and the State Pattern [modified] Pin
Guffa6-Jun-06 4:23
Guffa6-Jun-06 4:23 
GeneralRe: Object lifetime, GC and the State Pattern [modified] Pin
User 66586-Jun-06 4:28
User 66586-Jun-06 4:28 
AnswerRe: Object lifetime, GC and the State Pattern [modified] Pin
Guffa6-Jun-06 8:50
Guffa6-Jun-06 8:50 
QuestionString manipulation performance issue Pin
sjembek6-Jun-06 3:37
sjembek6-Jun-06 3:37 
AnswerRe: String manipulation performance issue [modified] Pin
sathish s6-Jun-06 3:48
sathish s6-Jun-06 3:48 
GeneralRe: String manipulation performance issue [modified] Pin
sjembek6-Jun-06 4:04
sjembek6-Jun-06 4:04 
AnswerRe: String manipulation performance issue Pin
Stefan Troschuetz6-Jun-06 3:50
Stefan Troschuetz6-Jun-06 3:50 
AnswerRe: String manipulation performance issue [modified] Pin
Guffa6-Jun-06 5:20
Guffa6-Jun-06 5:20 
When the += operator is used on a string, it might appear like the string is appended to the end of the original string. This is not true, as strings are immutable in .NET.

The statement:

content += " ";

is actually performed as:

content = string.Concat(content, " ");

With that in mind, let's do some math to find out why the routine is so slow:

Each iteration does either one, two or three concatenations. The first one is done every iteration, the second is done every other iteration, and the third is done every 16th iteration.

This gives that:

:: Each iteration does by average 1.5625 string concatenations.

:: Each iteration adds by average 3.0625 characters to the string.

With an array containing 125000 elements it produces a string that contains about 383000 characters. As each character is two bytes, that gives a string that uses 766 kbyte of data.

As the string is growing in a linear fashion, we can calcuate the average work done by each concatenation by taking the average size of the string during the operation, which is half the size of the finished string.

So a concatenation is by average moving an amount of 383 kbytes of data. As we have 125000 iterations, we have around 195000 string concatenations (125000 times 1.5625). 195000 times 383 kbytes makes 74685000 kbyte.

When the routine has finished, it has moved somewhere around 75 gigabyte of data. (As that is far more than the amount of avialable RAM, this has also caused hundreds of garbage collections to take place.)

That is the reason why the routine is so slow.


To improve the routine is easy. Use a StringBuilder. That would make the routine run around a 100000 times faster.


As an interresting observation in optimization, one can speed up the routine somewhat by using a temporary string:

string content, line;
content = string.Empty;
line = string.Empty;
for (int x = 0; x < Data.senderdata.Length; x++) {
	if (x % 16 == 0) {
		content += line;
		line = String.Format("\n{0:X5}: ", (x+1));
	}
	line += String.Format("{0:X2}", Data.senderdata[x]);
	if ((x + 1) % 2 == 0) line += " ";
}
content += line;


This would redude the number of lengthy concatenations from 1.5625 per iteration to 0.0625, reducing the execution time by 96%. Not nearly as effective as using a StringBuilder, but somewhat impressive eventhough... Smile | :)


---
b { font-weight: normal; }


-- modified at 11:27 Tuesday 6th June, 2006
QuestionControlled Focus event - textBox Pin
conrado76-Jun-06 3:26
conrado76-Jun-06 3:26 
AnswerRe: Controlled Focus event - textBox Pin
NaNg152416-Jun-06 3:34
NaNg152416-Jun-06 3:34 
GeneralRe: Controlled Focus event - textBox Pin
conrado76-Jun-06 3:45
conrado76-Jun-06 3:45 
QuestionException Pin
sanmech6-Jun-06 3:25
sanmech6-Jun-06 3:25 
QuestionDrag&Drop listview subitem? Pin
Dominik Reichl6-Jun-06 3:16
Dominik Reichl6-Jun-06 3:16 
Questionconnected to the internet or not ?? Pin
Tamimi - Code6-Jun-06 3:10
Tamimi - Code6-Jun-06 3:10 
AnswerRe: connected to the internet or not ?? Pin
stancrm6-Jun-06 3:31
stancrm6-Jun-06 3:31 
GeneralRe: connected to the internet or not ?? Pin
Tamimi - Code6-Jun-06 3:47
Tamimi - Code6-Jun-06 3:47 
GeneralRe: connected to the internet or not ?? Pin
stancrm6-Jun-06 3:55
stancrm6-Jun-06 3:55 
GeneralRe: connected to the internet or not ?? Pin
Aaron Dilliard6-Jun-06 3:57
Aaron Dilliard6-Jun-06 3:57 
QuestionKeyboard layout Pin
hany_hu6-Jun-06 2:58
hany_hu6-Jun-06 2:58 
AnswerRe: Keyboard layout Pin
stancrm6-Jun-06 3:03
stancrm6-Jun-06 3:03 
Questionhowto avoid double databinding ? help ??? Pin
cmpeng346-Jun-06 1:05
cmpeng346-Jun-06 1:05 
AnswerRe: howto avoid double databinding ? help ??? Pin
J4amieC6-Jun-06 1:17
J4amieC6-Jun-06 1:17 
AnswerRe: howto avoid double databinding ? help ??? Pin
rah_sin6-Jun-06 1:18
professionalrah_sin6-Jun-06 1:18 
Questiondatabinding problem ? help please. Pin
cmpeng346-Jun-06 0:53
cmpeng346-Jun-06 0:53 
AnswerRe: databinding problem ? help please. Pin
rah_sin6-Jun-06 1:12
professionalrah_sin6-Jun-06 1:12 

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.