in reply to Which is more faster? While or tr///
I have to calculate number of all letters in files and accordingly increase countYour code does not match your description. Your code counts the number of Ws.while ($string=~m{(W)}g){
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Which is more faster? While or tr///
by tej (Scribe) on Feb 01, 2011 at 09:37 UTC | |
| [reply] [d/l] |
by jwkrahn (Abbot) on Feb 01, 2011 at 11:05 UTC | |
I can see four problems with the posted code that would make it run slower:
For example:
Would be more efficient as:
That would cover points 1, 2 and 3. For point 4 you could use hash tables for the calculations, something like:
| [reply] [d/l] [select] |
by Anonyrnous Monk (Hermit) on Feb 01, 2011 at 10:19 UTC | |
Couldn't you just put all the weights in a lookup table, and then iterate once over the characters of the string? Something like this:
Or (if the string is huge)
(And if ord($ch) of the characters is within a narrow range (such as ASCII), you could also use an array, and store the weights under $array[ord($ch)] — which might be a tad faster than a hash.) | [reply] [d/l] [select] |
by JavaFan (Canon) on Feb 01, 2011 at 10:37 UTC | |
Whole code looks likeThat makes a tr/// solution a non-candidate, doesn't it? I'd probably go for something like: You may want to consider replacing the (.) with ($charclass), where: Whether that makes a difference depends on your data set. | [reply] [d/l] [select] |