[Israel.pm] what do you think is best
Shlomi Fish
shlomif at iglu.org.il
Thu Nov 25 00:36:49 PST 2010
Hi Chanan,
why didn't you reply to the list as I specifically request in my signature,
and there was also a Reply-to: header to guide you? I'm CCing this to the
list, because you have not specifically said that you are replying to me in
private, and I don't see anything private there. I wish GMail.com would burn
in the eternal fires of hell.
You've also top-posted.
On Thursday 25 November 2010 09:26:14 Chanan Berler wrote:
> Hi Shlomi,
>
> Thanks for your remarks.
> As for them:
>
> cunk_size -- oops misspelled, since i wrote the example first hand on
> this email.
So how do you know it works?
> no need for \G or \g since the text is single line only (it's a join
> of array values, seperated with , characters).
Now that I tried it I see that your program behaves as expected:
<<<
#!/usr/bin/perl
use strict;
use warnings;
my @all_bets = (1..200);
my $chunk_size = 7;
my $bet_txt = join ",", @all_bets;
$bet_txt .= ",";
my $cnt = 0;
my $group;
while (($group) = $bet_txt =~ /((?:\d+,){1,$chunk_size})/o)
{
print $group . "\n";
$bet_txt = $';#'
exit if ($cnt++ == 10);
}
>>>
However, doing $bet_txt = $' is an inefficient way to do it and here's what
perldoc perlvar is saying about $' :
<<<<
$' The string following whatever was matched by the last
successful pattern match (not counting any matches hidden
within a BLOCK or eval() enclosed by the current BLOCK).
(Mnemonic: "'" often follows a quoted string.) Example:
local $_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # prints abc:def:ghi
This variable is read-only and dynamically scoped to the
current BLOCK.
The use of this variable anywhere in a program imposes a
considerable performance penalty on all regular expression
matches. See "BUGS".
See "@-" for a replacement.
>>>>
So you should not use it. Here is a better program using \G and /g:
[perl]
#!/usr/bin/perl
use strict;
use warnings;
my @all_bets = (1..200);
my $chunk_size = 7;
my $bet_txt = join ",", @all_bets;
$bet_txt .= ",";
my $cnt = 0;
my $group;
while ($bet_txt =~ /\G((?:\d+,){1,$chunk_size})/og)
{
print $1, "\n";
exit if ($cnt++ == 10);
}
[/perl]
>
> my question, is based on performance only, where using index on arrays
> vs. using regular expression for finding the chuck size (X number of
> elements).
A regular expression match is time consuming too.
> I was told, that regarding system resource, when applaying a buffer
> size (for the array string), will cost the system it's memory.
What is an array string?
> while using index based on an array it's less memory "expencive"
> what you think ?
Let's see for ourselves:
[Perl code]
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all :hireswallclock);
my $step_size = 7;
open my $null_fh, ">", "/dev/null";
sub using_splice
{
my @all_bets = (1..200);
while (@all_bets)
{
print {$null_fh} join( ',', splice(@all_bets, 0, $step_size) ), ",\n";
}
}
sub using_regex
{
my @all_bets = (1..200);
my $bet_txt = join ",", @all_bets;
$bet_txt .= ",";
while ($bet_txt =~ /\G((?:\d+,){1,$step_size})/og)
{
print {$null_fh} $1, "\n";
}
}
# using_splice();
# using_regex();
timethese(10_000,
{
'using_regex' => \&using_regex,
'using_splice' => \&using_splice,
}
);
close($null_fh);
[/Perl code]
This gives me the following results:
Benchmark: timing 10000 iterations of using_regex, using_splice...
using_regex: 3.22811 wallclock secs ( 3.14 usr + 0.01 sys = 3.15 CPU) @
3174.60/s (n=10000)
using_splice: 2.46572 wallclock secs ( 2.40 usr + 0.01 sys = 2.41 CPU) @
4149.38/s (n=10000)
So the array and splice version is faster.
We can also try natatime from List::MoreUtils.
Regards,
Shlomi Fish
>
> thanks
> Chanan
>
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
More information about the Perl
mailing list