Benchmarking [was] Re: Not Matt's Scripts
At 13:29 27/03/2001 +, Matthew Byng-Maddick wrote: my @th=(qw(th st nd rd),("th")x16)x2; $th[31]="st"; That's an evil and gross hack. [snip] sub th{(($_[0]-10-$_[0]%10)/10%10)?(qw(th st nd rd),('th')x6)[$_[0]%10]:"th"} The first one I understood. Not sure about the second but I'll work it out ;-) I thought I would play around with Benchmark.pm, because I don't use it nearly often enough, so I made this script: #! /usr/bin/perl -w use strict; use Benchmark; use POSIX 'strftime'; use vars qw(@th); @th=(qw(th st nd rd),("th")x16)x2; $th[31]="st"; sub th{(($_[0]-10-$_[0]%10)/10%10)?(qw(th st nd rd),('th')x6)[$_[0]%10]:"th"} my $count=10; timethese($count, { 'Array' = '{ my @time=localtime; my $dummy = strftime("%e$th[$time[3]] %b %Y\n", @time); }', 'Sub' = '{ my @time=localtime; my $th=th($time[3]); my $dummy = strftime("%e$th %b %Y\n", @time); }' }); Now - I don't know if I've used this right at all - comments and criticisms gladly accepted. The output is: Benchmark: timing 10 iterations of Array, Sub... Array: 3 wallclock secs ( 3.33 usr + 0.09 sys = 3.42 CPU) Sub: 6 wallclock secs ( 5.27 usr + 0.06 sys = 5.33 CPU) So - Did I get this heinously wrong or is MBM's sub really a lot slower ? Simon.
Re: Benchmarking [was] Re: Not Matt's Scripts
Simon Wilcox wrote: So - Did I get this heinously wrong or is MBM's sub really a lot slower ? Well, remember that the sub effecticaly recalculates (what amounts to) the array each time. To be fair, you should include the array initialisation inside the loop and see who wins then. Cheers, Philip -- Philip Newton [EMAIL PROTECTED] All opinions are my own, not my employer's. If you're not part of the solution, you're part of the precipitate.
Re: Benchmarking [was] Re: Not Matt's Scripts
On Tue, Mar 27, 2001 at 04:19:08PM +0100, Simon Wilcox wrote: I thought I would play around with Benchmark.pm, because I don't use it nearly often enough, so I made this script: @th=(qw(th st nd rd),("th")x16)x2; $th[31]="st"; sub th{(($_[0]-10-$_[0]%10)/10%10)?(qw(th st nd rd),('th')x6)[$_[0]%10]:"th"} Benchmark: timing 10 iterations of Array, Sub... Array: 3 wallclock secs ( 3.33 usr + 0.09 sys = 3.42 CPU) Sub: 6 wallclock secs ( 5.27 usr + 0.06 sys = 5.33 CPU) So - Did I get this heinously wrong or is MBM's sub really a lot slower ? No, you got it right. You would expect the sub version to be slower, as it has to make a subroutine call and do some calculations every time, whereas the array version pre-calculates everything and then just does a tonne of comparitively inexpensive array lookups. -- David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/ This is a signature. There are many like it but this one is mine. ** I read encrypted mail first, so encrypt if your message is important ** PGP signature
Re: Benchmarking [was] Re: Not Matt's Scripts
On Tue, Mar 27, 2001 at 05:40:19PM +0200, Philip Newton wrote: Well, remember that the sub effecticaly recalculates (what amounts to) the array each time. To be fair, you should include the array initialisation inside the loop and see who wins then. Hey, that's not _fair_! The whole point of using an array is that you can pre-populate it. (also it's more concise, and I find it more comprehensible. YMMV) .robin. -- Beware. The paranoids are watching you.
Re: Benchmarking [was] Re: Not Matt's Scripts
At 16:53 27/03/2001 +0100, Robin Houston wrote: On Tue, Mar 27, 2001 at 05:40:19PM +0200, Philip Newton wrote: Well, remember that the sub effecticaly recalculates (what amounts to) the array each time. To be fair, you should include the array initialisation inside the loop and see who wins then. Hey, that's not _fair_! The whole point of using an array is that you can pre-populate it. (also it's more concise, and I find it more comprehensible. YMMV) I agree, it's how I would have done it. I was just trying to see it really deserved the label "evil and gross hack". It seems to me that it doesn't but as you say, YMMV and I got to practice my benchmarking :-) Simon.
Re: Benchmarking [was] Re: Not Matt's Scripts
On Tue, 27 Mar 2001, Simon Wilcox wrote: At 16:53 27/03/2001 +0100, Robin Houston wrote: On Tue, Mar 27, 2001 at 05:40:19PM +0200, Philip Newton wrote: Well, remember that the sub effecticaly recalculates (what amounts to) the array each time. To be fair, you should include the array initialisation inside the loop and see who wins then. Hey, that's not _fair_! The whole point of using an array is that you can pre-populate it. (also it's more concise, and I find it more comprehensible. YMMV) I agree, it's how I would have done it. I was just trying to see it really deserved the label "evil and gross hack". Because it was only meant to deal with things up to 31, if you try and (for some reason) try to put 32 in, you would get 32th (because it has populated the array). I don't like that kind of thing. It's a personal choice. I think the bit I objected to most was the $th[31]="st" bit. I shouldn't have put it like that, but as Robin says, TIMTOWTDI,so yeah. It seems to me that it doesn't but as you say, YMMV and I got to practice my benchmarking :-) :) I did expect it to be slower, it also copes with any number. MBM -- Matthew Byng-Maddick Home: [EMAIL PROTECTED] +44 20 8980 5714 (Home) http://colondot.net/ Work: [EMAIL PROTECTED] +44 7956 613942 (Mobile) Knebel's Law: It is now proved beyond doubt that smoking is one of the leading causes of statistics.