tej has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I have to calculate number of all letters in files and accordingly increase count
I am using while loop for this
Does this makes my program little slow?... In such cases which one would be more faster tr/// or while?while ($string=~m{(W)}g){ $count=$count+($tbsz*0.7844); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Which is more faster? While or tr///
by GrandFather (Saint) on Feb 01, 2011 at 06:05 UTC | |
I would expect tr/// to be more fasterest. However, if it really matters you should write some code to benchmark the alternatives using Benchmark. The real question though is: "Does it actually matter?". Unless the code fragment has been identified using a profiler as the time hog in code that is running too slowly to achieve some particular goal, write the fragment in the way that seems clearest and easiest to maintain.
True laziness is hard work
| [reply] |
by tej (Scribe) on Feb 01, 2011 at 06:53 UTC | |
| [reply] |
|
Re: Which is more faster? While or tr///
by BrowserUk (Patriarch) on Feb 01, 2011 at 06:25 UTC | |
You're counting the number of 'W's? Or is that meant to be non-word characters: m[(\W)]g? Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Re: Which is more faster? While or tr///
by ikegami (Patriarch) on Feb 01, 2011 at 07:53 UTC | |
You tell us. If you can't tell, does it matter? Here are some clean implementations:
| [reply] [d/l] [select] |
|
Re: Which is more faster? While or tr///
by JavaFan (Canon) on Feb 01, 2011 at 07:39 UTC | |
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. | [reply] [d/l] |
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] |