Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
hmmmz waarom verbaast me dat nietOp vrijdag 14 december 2001 03:13 schreef mbravenboer het volgende:
OiSyn: in je oplossing zit een fout... Zal ff kijken wat er mis is. Hij is bovendien ERG langzaam.
.edit: ik zie nu waarom ie fout is, er moet ergens nog een vpos++ tussen... zal m effe editten <-- onzin, staat er wel
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
1
2
3
4
5
6
7
8
9
10
11
| int stopPos; int findStringLength = findString.length(); int replaceStringLength = replaceString.length(); startPos = source.indexOf(findString,startPos); while(startPos>-1&&startPos<=stopPos) { stopPos = startPos+length; source.replace(startPos,stopPos,replaceString); startPos = startPos+replaceStringLength; startPos = source.indexOf(findString,startPos); } |
volgens mij is ie nu wel een stuk sneller
Hier is mijn code (sorry voor de truck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| public static String replaceUltraSnel(String source, String firstString, String newString) { return replaceUltraSnel(new StringBuffer(source), firstString, newString).toString(); } public static StringBuffer replaceUltraSnel(StringBuffer source, String firstString, String newString) { final int firstStringLength = firstString.length(); final int newStringLength = firstString.length(); final char[] firstChars = firstString.toCharArray(); final char[] newChars = newString.toCharArray(); if(firstString.length() == newString.length()) { int max = source.length() - firstStringLength; boolean equals = false; for(int i=0; i < max; i++) { equals = true; for(int j=0;j < firstStringLength; j++) { if(source.charAt(i + j) != firstChars[j]) { equals = false; break; } } if(equals) { for(int j=0;j < firstStringLength; j++) { source.setCharAt(i + j, newChars[j]); } i += firstStringLength - 1; } } } else { int max = source.length() - firstStringLength; int dif = newString.length() - firstStringLength; boolean equals = false; for(int i=0; i < max; i++) { equals = true; for(int j=0;j < firstStringLength; j++) { if(source.charAt(i + j) != firstChars[j]) { equals = false; break; } } if(equals) { source.replace(i, i + firstStringLength, newString); i += newStringLength - 1; max += dif; } } } return source; } |
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
stom koppiepeestfoutje: char chNaar[] = van.toCharArray ();
moet natuurlijk zijn char chNaar[] = naar.toCharArray ();
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
stopPos = startPos+length;
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
he..maar windows is wel het meest gebruike os hoor!Op vrijdag 14 december 2001 03:20 schreef OiSyN het volgende:
alarmnummer gebruikt waarschijnlijk een compiler met fuzzy logic, die probeert te raden wat de domme user nou eigenlijk bedoelt
1
2
3
4
5
6
7
8
9
10
| int findStringLength = findString.lenght(); int replaceStringLength = replaceString.length(); startPos = source.indexOf(findString,startPos); while(startPos>-1&&startPos<=stopPos) { source.replace(startPos,startPos+findStringLength,replaceString); startPos = startPos+replaceStringLength; startPos = source.indexOf(findString,startPos); } |
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
5
6
7
8
9
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 950 OiSyn: 47871 mbravenboer: 355 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 502 OiSyn: 42950 mbravenboer: 480 |
Het is er dus niet echt beter op geworden Alarmnummer... Laat ik maar niets over OiSyn zeggen
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Alarmnummer: 489
OiSyn: 705
mbravenboer: 338
Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) :
Alarmnummer: 476
OiSyn: 688
mbravenboer: 491
of de test is veranderd..
Hum tja... ik zal OiSyn er even tussen-uit halen, jij komt namelijk daarna en hij gooit nogal met arrays...Alarmnummer: het lijkt me stug dat mijn code langzamer is geworden.
Moment
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
5
6
7
8
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 493 OiSyn: 1 mbravenboer: 339 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 488 OiSyn: 0 mbravenboer: 477 |
Echt sneller is hij dus niet geworden... Die 900 zal wel met het opruimen van de zooi van OiSyn te maken hebben
Ik zal ons tweeen nog even omdraaien.
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
5
6
7
8
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 536 OiSyn: 0 mbravenboer: 300 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 483 OiSyn: 0 mbravenboer: 494 |
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Komt OiSyn nog met een nieuwe kandidaat?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Zou wel kunnen, maar niet bewust. Waar doel je op? Overigens is jouw code voor het 'normale geval' iets sneller...Alarmnummer: ok.. jouw code is snellermaar je hebt gebruik gemaakt van dat algoritme waar laatst naar verwezen werd in mijn topic: String(Buffer) en char[] lib maken?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Ik heb hierbij (1) niet gebruik gemaakt van caching van de gecompileerde regex (2) gebruik gemaakt van de gecompileerde regex (3) gebruik gemaakt van de Matcher.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 554 OiSyn: 0 mbravenboer: 310 regex: 1506 regex (met Pattern cache): 1231 regex (met Matcher cache): 1149 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 494 OiSyn: 0 mbravenboer: 464 regex: 1444 regex (met Pattern cache): 1258 regex (met Matcher cache): 1171 |
De resultaten van de Matcher vielen mij eigenlijk wel een beetje tegen.
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| char[] findCharArray = findString.toArray();?? int findStringLength = findCHarArray.length; int replaceStringLength = replaceString.length(); startPos = indexOf(source,findCharArray,startPos); while(startPos>-1&&startPos<=stopPos) { source.replace (startPos,startPos+findStringLength,replaceString); startPos = startPos+replaceStringLength; startPos = indexOf(source,findCharArray,startPos); } public static int indexOf(StringBuffer source, char[] findCharArray, int startOffset) { int sourceIndex = startOffset; int findIndexInSource = -1; int findIndex = 0; boolean found = false; int stopOffset = source.length(); while(sourceIndex<stopOffset) { char charSource = source.charAt(sourceIndex); char charFind = findCharArray[findIndex]; if(charSource==charFind) { if(findIndexInSource == -1) { //1e goeie karakter ben je tegen gekomen. findIndexInSource = sourceIndex; } findIndex++; if(findIndex == findCharArray.length) { //je hebt de hele string gevonden sourceIndex = stopOffset; found = true; } } else { findIndexInSource = -1; findIndex = 0; } sourceIndex++; } if(found) return findIndexInSource; else return -1; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 541 OiSyn: 0 mbravenboer: 298 regex: 1520 regex (met Pattern cache): 1270 regex (met Matcher cache): 1143 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 459 OiSyn: 0 mbravenboer: 476 regex: 1459 regex (met Pattern cache): 1253 regex (met Matcher cache): 1177 |
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 496 OiSyn: 0 mbravenboer: 308 regex: 1512 regex (met Pattern cache): 1232 regex (met Matcher cache): 1119 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 467 OiSyn: 0 mbravenboer: 476 regex: 1417 regex (met Pattern cache): 1259 regex (met Matcher cache): 1153 |
Zou onze topic-starter ondertussen geholpen zijn?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Profiler zou hier inderdaad wel makkelijk zijn, maar ik denk niet dat we nog fundamenteel iets kunnen verbeteren in Java. De oplossing met de StringBuffer moet het snelste zijn en je ziet ook dat onze twee oplossingen ongeveer gelijk preseteren.Alarmnummer: profiler
Goed idee, wellicht zal ik vanavond ff een test draaienAlarmnummer: html pagina
Lullig voor je dat je te laat was
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
yeah ik ben het snelst!!!Op vrijdag 14 december 2001 03:44 schreef mbravenboer het volgende:
Hum ik heb jou nu eerste gezet (ipv mij) maar daar heb je niet echt voordeel van...
code:
1 2 3 4 5 6 7 8 Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 536 OiSyn: 0 mbravenboer: 300 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 483 OiSyn: 0 mbravenboer: 494
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
ik bedoel, StringBuffer werkt intern toch ook met char arrays? of is dat native geimplementeerd? Of heb ik ergens een gigantische fout zitten waarom het gewoon niet goed loopt?
Enlighten me please
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
jouw code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| while (tpos < tlen) { char t = chText[tpos++]; if (t != chVan[vpos]) { if (vpos > 0) { System.arrayCopy (chVan, vpos, buffer, bufpos, vpos);<==hier een.. bufpos += vpos; vpos = 0; } else buffer[bufpos++] = t; } else if (++vpos == vlen) { System.arrayCopy (chNaar, 0, buffer, bufpos, nlen);<=weer een! |
ik ging ervan uit dat System.arrayCopy sneller was dan zelf die arrays doorlopen, waarom zou die feature er anders in zitten
ik kan ze wel zelf gaan kopieren mbv een for-lusje, maar volgens mij wordt het er niet echt bepaald sneller op
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
newLengte=oudLengte+lijst.size()*(replaceString.length-findString.length());
en dan ga je alles in de nieuwe stringbuffer/array plaatsen.
maarja..of je door al dat gecopieer er nou op vooruit gaat?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
| /** * CharArrayUtilities.java * * @author Peter Veentjer */ package com.jph.string; public class CharArrayUtilities { public static int indexOfIgnoreCase(char[] source,char[] target) { return indexOfIgnoreCase(source,target,0,source.length); } public static int indexOfIgnoreCase(char[] source,char[] target, int fromIndex) { return indexOfIgnoreCase(source,target,fromIndex,source.length); } public static int indexOfIgnoreCase(char[] source,char[] target, int fromIndex, int stopOffset) { return indexOfIgnoreCase(source,0,stopOffset,target,0,target.length,fromIndex); } public static int indexOf(char[] source,char[] target) { return indexOf(source,target,0,source.length); } public static int indexOf(char[] source,char[] target, int fromIndex) { return indexOf(source,target,fromIndex,source.length); } public static int indexOf(char[] source,char[] target, int fromIndex, int stopIndex) { return indexOf(source,0,stopIndex,target,0,target.length,fromIndex); } public static int lastIndexOf(char[] source,char[] target) { return lastIndexOf(source,target,source.length-1,0); } public static int lastIndexOf(char[] source,char[] target, int fromIndex) { return lastIndexOf(source,target,fromIndex,0); } public static int lastIndexOf(char[] source,char[] target, int fromIndex, int stopIndex) { return lastIndexOf(source,stopIndex,source.length-stopIndex,target,0,target.length,fromIndex); } public static int lastIndexOfIgnoreCase(char[] source,char[] target) { return lastIndexOfIgnoreCase(source,target,source.length-1,0); } public static int lastIndexOfIgnoreCase(char[] source,char[] target, int fromIndex) { return lastIndexOfIgnoreCase(source,target,fromIndex,0); } public static int lastIndexOfIgnoreCase(char[] source,char[] target, int fromIndex, int stopIndex) { return lastIndexOfIgnoreCase(source,stopIndex,source.length-stopIndex,target,0,target.length,fromIndex); } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * * @param sourceOffset offset of the source string. * * @param sourceCount count of the source string. * * @param target the characters being searched for. * * @param targetOffset offset of the target string. * * @param targetCount count of the target string. * * @param fromIndex the index to begin searching from. */ static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; //de huidige karakter positie int i = sourceOffset + fromIndex; //de maximale offset. int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar: while (true) { /* Look for first character. */ while (i <= max && source[i] != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (source[j++] != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } static int indexOfIgnoreCase(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } //converteer target naar lower case. char[] lowerCaseTarget = new char[target.length]; for(int k=0;k<target.length;k++) lowerCaseTarget[k]=Character.toLowerCase(target[k]); char first = lowerCaseTarget[targetOffset]; //de huidige karakter positie int i = sourceOffset + fromIndex; //de maximale offset. int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar: while (true) { /* Look for first character. */ while (i <= max && Character.toLowerCase(source[i]) != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (Character.toLowerCase(source[j++]) != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * @param sourceOffset offset of the source string. * @param sourceCount count of the source string. * @param target the characters being searched for. * @param targetOffset offset of the target string. * @param targetCount count of the target string. * @param fromIndex the index to begin searching from. */ static int lastIndexOfIgnoreCase(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { /* * Check arguments; return immediately where possible. For * consistency, don't check for null str. */ int rightIndex = sourceCount - targetCount; if (fromIndex < 0) { return -1; } if (fromIndex > rightIndex) { fromIndex = rightIndex; } /* Empty string always matches. */ if (targetCount == 0) { return fromIndex; } //converteer target naar lower case. char[] lowerCaseTarget = new char[target.length]; for(int k=0;k<target.length;k++) lowerCaseTarget[k]=Character.toLowerCase(target[k]); int strLastIndex = targetOffset + targetCount - 1; char strLastChar = lowerCaseTarget[strLastIndex]; int min = sourceOffset + targetCount - 1; int i = min + fromIndex; startSearchForLastChar: while (true) { while (i >= min && Character.toLowerCase(source[i]) != strLastChar) { i--; } if (i < min) { return -1; } int j = i - 1; int start = j - (targetCount - 1); int k = strLastIndex - 1; while (j > start) { if (Character.toLowerCase(source[j--]) != lowerCaseTarget[k--]) { i--; continue startSearchForLastChar; } } return start - sourceOffset + 1; } } static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { /* * Check arguments; return immediately where possible. For * consistency, don't check for null str. */ int rightIndex = sourceCount - targetCount; if (fromIndex < 0) { return -1; } if (fromIndex > rightIndex) { fromIndex = rightIndex; } /* Empty string always matches. */ if (targetCount == 0) { return fromIndex; } int strLastIndex = targetOffset + targetCount - 1; char strLastChar = target[strLastIndex]; int min = sourceOffset + targetCount - 1; int i = min + fromIndex; startSearchForLastChar: while (true) { while (i >= min && source[i] != strLastChar) { i--; } if (i < min) { return -1; } int j = i - 1; int start = j - (targetCount - 1); int k = strLastIndex - 1; while (j > start) { if (source[j--] != target[k--]) { i--; continue startSearchForLastChar; } } return start - sourceOffset + 1; } } public static void main(String[] args) { String source = "peter"; String target = "e"; int indexOf = lastIndexOf(source.toCharArray(),target.toCharArray(),10); System.out.println("indexOf:"+indexOf); } } |
Ik zal hem vanavond ff bench-marken
OiSyn: ik vermoed dat de slechte performance komt door de grote char[] .... maar dat is nogal een gok
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
als je de buffer nou eens static maakt (is ie wel niet thread-safe, maar het is alleen maar om te testen
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
jdk1.4beta3fast(crash)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
| /** * Test.java * * @author Created by Peter Veentjer */ package com.jph.string; import java.util.*; public class Test { /** * Vervangt in source alle voorkomens van findString door replaceString. * * @param source de StringBuffer waarin vervangen moet worden. * @param findString de te zoeken String. * @param replaceString de te vervangen String. * @param startPos de positie vanaf waar je begint te vervangen. * @param stopPos de eerste positie die niet vervangen meer mag worden */ public static void replaceFast(StringBuffer source,String findString, String replaceString, int startPos, int stopPos) { if(findString.length()==0) throw new NullPointerException("findString.length() must be greater than 0"); if(stopPos<0) throw new IndexOutOfBoundsException("stopPos must be greater or equal to zero, stopPos = "+stopPos); if(stopPos>source.length()) stopPos = source.length(); if(startPos<0) throw new IndexOutOfBoundsException("startPos can`t be smaller than 0"); if(startPos>=stopPos) throw new IndexOutOfBoundsException("startPos must be smaller than stopPos, startPos="+startPos+" stopPos="+stopPos); char[] replaceCharArray = replaceString.toCharArray(); int replaceCharArrayLength = replaceString.length(); //converteer de findstring naar array char[] findCharArray = findString.toCharArray(); int findCharArrayLength =findCharArray.length; //copieer de stringbuffer naar string. char[] sourceCharArray = new char[source.length()]; source.getChars(0,source.length(),sourceCharArray,0); //hierin komen alle startpos van find string te staan. ArrayList list = new ArrayList(); //vul de list met alle startPosities posities startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,0); while(startPos>-1&&startPos<=stopPos) { list.add(new Integer(startPos)); startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,startPos+findCharArrayLength); } //controleer of er iets gevonden is. if(list.size()==0) return; //bereken de nieuwe buffer lengte. int newBufferLengte = source.length()+list.size()*(replaceCharArrayLength-findCharArrayLength); //maak buffer aan met nieuwe lengte char[] destCharArray = new char[newBufferLengte]; int listSize = list.size(); int destCharIndex = 0; int sourceCharIndex = 0; for(int k=0;k<listSize;k++) { startPos = ((Integer)list.get(k)).intValue(); int copyLengte = startPos - sourceCharIndex; System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray, destCharIndex,copyLengte); sourceCharIndex = startPos +findCharArrayLength; destCharIndex = destCharIndex + copyLengte; System.arraycopy(replaceCharArray,0,destCharArray,destCharIndex,replaceCharArrayLength); destCharIndex = destCharIndex + replaceCharArrayLength; } System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray,destCharIndex,sourceCharArray.length-sourceCharIndex); source.delete(0,source.length()); source.append(destCharArray); } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * * @param sourceOffset offset of the source string. * * @param sourceCount count of the source string. * * @param target the characters being searched for. * * @param targetOffset offset of the target string. * * @param targetCount count of the target string. * * @param fromIndex the index to begin searching from. */ static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; //de huidige karakter positie int i = sourceOffset + fromIndex; //de maximale offset. int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar: while (true) { /* Look for first character. */ while (i <= max && source[i] != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (source[j++] != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } public static void main(String[] args) { StringBuffer stringBuffer = new StringBuffer("peter"); String zoekString = "e"; replaceFast(stringBuffer,"e","aa",0,stringBuffer.length()); System.out.println("result:"); System.out.println(stringBuffer.toString()); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
| Resultaten bij een vervanging met zelfde lengte: Alarmnummer: 585 mbravenboer: 297 regex: 1497 regex (met Pattern cache): 1226 regex (met Matcher cache): 1397 Resultaten bij een vervanging met 1 teken meer (en enkele voorkomens) : Alarmnummer: 613 mbravenboer: 462 regex: 1426 regex (met Pattern cache): 1275 regex (met Matcher cache): 1190 |
Ikke ben nu dus duidelijk sneller
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Ik heb je laatste Test code genomen. De slechte prestatie kan natuurlijk door de test komen, maar uiteraard zou de oplossing het op alle punten beter moeten doen.
Je hebt ook best kan dat de mindere performance door de ArrayList wordt veroorzaakt?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
De ArrayList aanmaken en de while loop erna = indexen
Alles daarna = vervangen
1
2
| Indexen Alarmnummer: 799 Vervangen Alarmnummer: 1107 |
De oorzaak lijkt mij dus wel voor een deel bij de ArrayList liggen, maar je moet denk ik ook niet verwachten mega-performance te krijgen als je alleen dat oplost...
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
ZekersOiSyN: nou ik vond mijn 2e char-array oplossing anders ook wel elite
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Nope nog niets met HTML gedaan. Als ik m'n compiler ff zat ben zal ik het even tussendoor doen...Alarmnummer: heb je al een nieuwe benchmark klaar?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
't is op een strijd tussen javahova's en een java-wannabe-c++er uitgedraaid
AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!
ja hoor, droom maar lekker verder

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!
ONE TAKEN!no offense
en ga nu je excuses aanbieden aan mij
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
ik keek vanmiddag even of er al "een" antwoord zou zijn op mijn vraag; 87 replies! whaah
volgens mij heb ik hier een beetje een programmeerwedstrijd georganiseerd ofzow
in ieder geval zijn de antwoorden héél bruikbaar;
thnx 4 that!!
maar volgens mij zitten er fouten in.
als ik namelijk de volgende string heb in messageField (is een tekstvak)
1
| dit is een<I><U><B><I></I></B></U></I></I> markup</U> |
en ik laat er dan dit op los:
1
2
3
4
5
6
| String[] markuplist ={"<B>","</B>","<U>","</U>","<I>","</I>"}; String text = messageField.getText(); for(int i=0;i<Array.getLength(markuplist);i++){ text = MiscFunctions.replaceUltraSnel(text,markuplist[i],""); } messageField.setText(text); |
dan vervangt hij niet correct:
1
2
| ouput: dit is een<I></I> markup</U> |
over het algemeen werkt de code goed,
maar als ik veeeeel verschillende tags tegen elkaar aanzet in de tekst, mist hij er een paar! (niet altijd de <I> hoor, ook anderen...)
de laatste pakt hij sowieso nooit...
(het makkelijkst is voor mij namelijk:
public static String replaceFast(String text,String zoek, String vervang){}.
dan is het het makkelijkst toepasbaar vanuit elke klasse.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
| import java.util.*; public class MiscFunctions{ public MiscFunctions(){} public static void replaceFast(String Source, String findString, String replaceString) { StringBuffer source = new StringBuffer(Source); int stopPos = source.length(); int startPos = 0; if(findString.length()==0) throw new NullPointerException("findString.length() must be greater than 0"); if(stopPos<0) throw new IndexOutOfBoundsException("stopPos must be greater or equal to zero, stopPos = "+stopPos); if(stopPos>source.length()) stopPos = source.length(); if(startPos<0) throw new IndexOutOfBoundsException("startPos can`t be smaller than 0"); if(startPos>=stopPos) throw new IndexOutOfBoundsException("startPos must be smaller than stopPos, startPos="+startPos+" stopPos="+stopPos); char[] replaceCharArray = replaceString.toCharArray(); int replaceCharArrayLength = replaceString.length(); //converteer de findstring naar array char[] findCharArray = findString.toCharArray(); int findCharArrayLength =findCharArray.length; //copieer de stringbuffer naar string. char[] sourceCharArray = new char[source.length()]; source.getChars(0,source.length(),sourceCharArray,0); //hierin komen alle startpos van find string te staan. ArrayList list = new ArrayList(); //vul de list met alle startPosities posities startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,0); while(startPos>-1&&startPos<=stopPos) { list.add(new Integer(startPos)); startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,startPos+findCharArrayLength); } //controleer of er iets gevonden is. if(list.size()==0) return; //bereken de nieuwe buffer lengte. int newBufferLengte = source.length()+list.size()*(replaceCharArrayLength-findCharArrayLength); //maak buffer aan met nieuwe lengte char[] destCharArray = new char[newBufferLengte]; int listSize = list.size(); int destCharIndex = 0; int sourceCharIndex = 0; for(int k=0;k<listSize;k++) { startPos = ((Integer)list.get(k)).intValue(); int copyLengte = startPos - sourceCharIndex; System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray, destCharIndex,copyLengte); sourceCharIndex = startPos +findCharArrayLength; destCharIndex = destCharIndex + copyLengte; System.arraycopy(replaceCharArray,0,destCharArray,destCharIndex,replaceCharArrayLength); destCharIndex = destCharIndex + replaceCharArrayLength; } System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray,destCharIndex,sourceCharArray.length-sourceCharIndex); source.delete(0,source.length()); source.append(destCharArray); } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * * @param sourceOffset offset of the source string. * * @param sourceCount count of the source string. * * @param target the characters being searched for. * * @param targetOffset offset of the target string. * * @param targetCount count of the target string. * * @param fromIndex the index to begin searching from. */ static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; //de huidige karakter positie int i = sourceOffset + fromIndex; //de maximale offset. int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar: while (true) { /* Look for first character. */ while (i <= max && source[i] != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (source[j++] != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } } |
het moet dus wel een String returnen, en geen void zijn.
Dat is uitermate lstig als je van buiten de klasse zo'n vervang-functie wil aanroepen.
..dit newbietje weet niet precies waar hij dat:
return new String(source);
moet neerzetten
als ik het namelijk bij dat if-statement zet:
1
2
3
| //controleer of er iets gevonden is. if(list.size()==0) return new String(source); |
dan zegt de compiler dat er geen waarde wordt gereturnd.
(omdat het binnen een if-lus zit, weet java nooit zeker of hij ooit wel iets returned)
---EDIT----
I got it!
in de volgende post staatie!
(btw, deze werkt perfect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| /** * Test.java * * @author Created by Peter Veentjer */ public static String replaceFast(String Source, String findString, String replaceString) { StringBuffer source = new StringBuffer(Source); int stopPos = source.length(); int startPos = 0; if(findString.length()==0) throw new NullPointerException("findString.length() must be greater than 0"); if(stopPos<0) throw new IndexOutOfBoundsException("stopPos must be greater or equal to zero, stopPos = "+stopPos); if(stopPos>source.length()) stopPos = source.length(); if(startPos<0) throw new IndexOutOfBoundsException("startPos can`t be smaller than 0"); if(startPos>=stopPos) throw new IndexOutOfBoundsException("startPos must be smaller than stopPos, startPos="+startPos+" stopPos="+stopPos); char[] replaceCharArray = replaceString.toCharArray(); int replaceCharArrayLength = replaceString.length(); //converteer de findstring naar array char[] findCharArray = findString.toCharArray(); int findCharArrayLength =findCharArray.length; //copieer de stringbuffer naar string. char[] sourceCharArray = new char[source.length()]; source.getChars(0,source.length(),sourceCharArray,0); //hierin komen alle startpos van find string te staan. ArrayList list = new ArrayList(); //vul de list met alle startPosities posities startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,0); while(startPos>-1&&startPos<=stopPos) { list.add(new Integer(startPos)); startPos = indexOf(sourceCharArray,0,source.length(),findCharArray,0,findCharArrayLength,startPos+findCharArrayLength); } //controleer of er iets gevonden is. if(list.size()==0) return Source; //bereken de nieuwe buffer lengte. int newBufferLengte = source.length()+list.size()*(replaceCharArrayLength-findCharArrayLength); //maak buffer aan met nieuwe lengte char[] destCharArray = new char[newBufferLengte]; int listSize = list.size(); int destCharIndex = 0; int sourceCharIndex = 0; for(int k=0;k<listSize;k++) { startPos = ((Integer)list.get(k)).intValue(); int copyLengte = startPos - sourceCharIndex; System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray, destCharIndex,copyLengte); sourceCharIndex = startPos +findCharArrayLength; destCharIndex = destCharIndex + copyLengte; System.arraycopy(replaceCharArray,0,destCharArray,destCharIndex,replaceCharArrayLength); destCharIndex = destCharIndex + replaceCharArrayLength; } System.arraycopy(sourceCharArray,sourceCharIndex,destCharArray,destCharIndex,sourceCharArray.length-sourceCharIndex); source.delete(0,source.length()); source.append(destCharArray); return new String(source); } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * * @param sourceOffset offset of the source string. * * @param sourceCount count of the source string. * * @param target the characters being searched for. * * @param targetOffset offset of the target string. * * @param targetCount count of the target string. * * @param fromIndex the index to begin searching from. */ static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; //de huidige karakter positie int i = sourceOffset + fromIndex; //de maximale offset. int max = sourceOffset + (sourceCount - targetCount); startSearchForFirstChar: while (true) { /* Look for first character. */ while (i <= max && source[i] != first) { i++; } if (i > max) { return -1; } /* Found first character, now look at the rest of v2 */ int j = i + 1; int end = j + targetCount - 1; int k = targetOffset + 1; while (j < end) { if (source[j++] != target[k++]) { i++; /* Look for str's first char again. */ continue startSearchForFirstChar; } } return i - sourceOffset; /* Found whole string. */ } } |
hij werkt snel zat voor mijn doeleinde in ieder geval
thnx!!
btw, deze zouden ze standaard in de String library moeten opnemen vind ik. is een errug handig methodetje
en nu coden!!Op zaterdag 15 december 2001 22:51 schreef OiSyN het volgende:
ONE TAKEN!
en ga nu je excuses aanbieden aan mij![]()
AMD Phenom II X4 // 8 GB DDR2 // SAMSUNG 830 SSD // 840 EVO SSD // Daar is Sinterklaas alweer!!
Ik vermoed dat er ergens een index ofzo niet helemaal goed staat...
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Er is dus tegenwoordig een methode die dit doet (en nog veel meerZynth: deze zouden ze standaard in de String library moeten opnemen vind ik. is een errug handig methodetje
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
De fout die de topicstarter tegen kwam, had ik trouwens al hersteld in mijn eigen code.... Was nogal een stom probleem
Hier is de goede code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
| public class ReplaceTest { public static void main(String[] ps) { String text1 = "dit is een<I><U><B><I></I></B></U></I></I> markup</U>"; StringBuffer text2 = new StringBufer(text1); String[] markuplist ={"<B>","</B>","<U>","</U>","<I>","</I>"}; for(int i=0;i< markuplist.length; i++) { replaceUltraSnel(text2,markuplist[i],""); System.out.println("Na " + markuplist[i] + ", resultaat = " + text2.toString()); } } public static String replaceUltraSnel(String source, String firstString, String newString) { return replaceUltraSnel(new StringBuffer(source), firstString, newString).toString(); } public static StringBuffer replaceUltraSnel(StringBuffer source, String firstString, String newString) { final int firstStringLength = firstString.length(); final int newStringLength = newString.length(); final char[] firstChars = firstString.toCharArray(); final char[] newChars = newString.toCharArray(); if(firstString.length() == newString.length()) { int max = source.length() - firstStringLength; boolean equals = false; for(int i=0; i < max; i++) { equals = true; for(int j=0;j < firstStringLength; j++) { if(source.charAt(i + j) != firstChars[j]) { equals = false; break; } } if(equals) { for(int j=0;j < firstStringLength; j++) { source.setCharAt(i + j, newChars[j]); } i += firstStringLength - 1; } } } else { int max = source.length() - firstString.length() + 1; int dif = newString.length() - firstStringLength; boolean equals = false; for(int i=0; i < max; i++) { equals = true; for(int j=0;j < firstStringLength; j++) { if(source.charAt(i + j) != firstChars[j]) { equals = false; break; } } if(equals) { source.replace(i, i + firstStringLength, newString); i = i + newStringLength - 1; max = max + dif; } } } return source; } } |
Overigens kan je veel beter gebruik maken van de StringBuffer methode ipv steeds de String methode aan te roepen, zie hierboven. Daar is de StringBuffer methode namelijk voor bedoeld
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
1
2
3
4
| public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } |
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
En bere traag, zie mijn eerdere bench-markstomato :Ach hoe schoon
Bij herhaling van dezelfde replace zou het een stuk sneller moeten kunnen. Die code zuigt werkelijk IMHO
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Ja heb je helemaal gelijk in natuurlijk, ik zat maar te dollenmbravenboer: Bij herhaling van dezelfde replace zou het een stuk sneller moeten kunnen. Die code zuigt werkelijk IMHO.
Valt me trouwens best tegen dat dergelijke functies echt heel inefficient geimplementeerd blijken
Voor deze keer dantomato: Ja heb je helemaal gelijk in natuurlijk, ik zat maar te dollen
Ja mij ookValt me trouwens best tegen dat dergelijke functies echt heel inefficient geimplementeerd blijken
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Op zich is het wel leuk om die Matcher te optimaliseren
Wat vond je trouwens van mijn String - transformatie taaltje wat ik pas voorstelde als alternatief voor een String lib? Je kunt dit zelfs taal-onafhankelijk maken en naar verschillende talen gaan compileren
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
oja.. voor de liefhebbers.. ik ben al een tijdje bezig met een nieuwe site, (oude was erughhh lelijk
http://www.geocities.com/peter_veentjer
ps. de site is nog lang niet klaar.. maar het gaat om het idee..
Je bedoelt dat taaltje? Dat denk ik dus juist welAlarmnummer: ik denk niet dat ze zo snel worden als low level werk.
Cooloja.. voor de liefhebbers.. ik ben al een tijdje bezig met een nieuwe site, (oude was erughhh lelijk) en heb een tool gemaakt die een boom toevoegd op een html pagina op basis van xml bestandje..
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Zef: we moesten je voordragen voor de Nobelprijs voor Java innovatie
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
http://www.pandoramix.org niet voor niets uit m'n profile gehaaldAlarmnummer: waar is jouw site??
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Hum tja, misschien is dat wel weer zo, maar heb je dan genoeg functies om een lib te vullen?Alarmnummer: ik denk dat de meeste mensen in 90% van de gevallen belangstelling hebben bij eenvoudige replace/find routines.
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
Verwijderd
Maar waar kiezen jullie normaal voor als het gaat om optimalisatie: geheugen of snelheid? (zit hier namelijk vaak mee)
Is er ergens een artikel beschreven (op het net) die een soort algemene richtlijn/aanwijzing aangeeft?
Meestal komt dit toch op hetzelfde neer bij Java: het maken van nieuwe objecten kost erg veel tijd en uiteraard ook geheugen. Object creatie beperken is dus het aller-belangrijkste en als je dat voorkomt sla je twee vliegen in 1 klap.tijnbraun: Maar waar kiezen jullie normaal voor als het gaat om optimalisatie: geheugen of snelheid? (zit hier namelijk vaak mee)
Dit is een erg goed boek over Java Performance: Java Platform Performance: Strategies and Tactics. Je kan twee voorbeeld hoofdstukken downloadenIs er ergens een artikel beschreven (op het net) die een soort algemene richtlijn/aanwijzing aangeeft?
Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment
over het algemeen optimaliseer ik nooit (hoogstends algoritme optimalisatie = macro optimalisatie). En als ik moet kiezen dan ga ik voor het besparen van cpu gebruik, gewoon hopen dat er genoeg geheugen isOp zondag 16 december 2001 20:07 schreef tijnbraun het volgende:
compleet offtopic...
Maar waar kiezen jullie normaal voor als het gaat om optimalisatie: geheugen of snelheid? (zit hier namelijk vaak mee)
Is er ergens een artikel beschreven (op het net) die een soort algemene richtlijn/aanwijzing aangeeft?
Verwijderd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| public String replaceString(String original, String pattern, String replace) { int originalLength = original.length(); int patternLength = pattern.length(); int lastIndex = 0, index = 0; StringBuffer result = new StringBuffer(originalLength * 2); char[] chars = new char[originalLength]; while((index = original.indexOf(pattern, lastIndex)) >= 0) { original.getChars(lastIndex, index, chars, 0); result.append(chars, 0, index - lastIndex).append(replace); lastIndex = index + patternLength; } original.getChars(lastIndex, originalLength, chars, 0); result.append(chars, 0, originalLength - lastIndex); return result.toString(); } |
Als jullie die nou es in de benchmark gooien
Grtz
Dark
edit: ik weet niet hoe ik kan linken naar een topic maar het noemt: [java] code optimalisatie Daarin veel code en vele algo's - het kwam voort uit dit topic.
[ Voor 26% gewijzigd door hobbit_be op 12-09-2003 17:35 ]
Ik doe 'm even op slot, zodat ie weg kan zakken. Ik gooi 'm later weer open, maar mocht je deze sluitmessage lezen terwijl ie nog op slot zit, en we zijn alweer een tijdje verder, en je hebt wat interessants toe te voegen, mail me dan
.edit: woei ik dacht eraan
[ Voor 20% gewijzigd door .oisyn op 14-09-2003 14:13 ]
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.