Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Good way to un-chomp an array

Reply
Thread Tools

Good way to un-chomp an array

 
 
Suresh Govindachar
Guest
Posts: n/a
 
      08-13-2004
Hello,

What is a good way (with respect to speed)
to un-chomp an array?

for(my $i=0; $i<scalar @lines; $i++)
{
$lines[$i] .= "\n";
}

Thanks,

--Suresh


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-13-2004
Suresh Govindachar wrote:
> What is a good way (with respect to speed)
> to un-chomp an array?
>
> for(my $i=0; $i<scalar @lines; $i++)
> {
> $lines[$i] .= "\n";
> }


This is simpler code:

print map "$_\n", @lines;

As regards speed, I have no idea. If that is a concern of yours, do a
benchmark ("perldoc Benchmark").

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Scott W Gifford
Guest
Posts: n/a
 
      08-13-2004
"Suresh Govindachar" <(E-Mail Removed)> writes:

> Hello,
>
> What is a good way (with respect to speed)
> to un-chomp an array?


This was the fastest way I could come up with:

$_ .= "\n"
foreach @lines;

I benchmarked it, your original, and a map-based solution, and found
it was about 30% faster than your original, and about twice as fast as
map:

Benchmark: timing 50 iterations of for_index, foreach, map...
for_index: 18 wallclock secs (17.44 usr + 0.31 sys = 17.75 CPU) @ 2.82/s (n=50)
foreach: 13 wallclock secs (12.54 usr + 0.17 sys = 12.71 CPU) @ 3.93/s (n=50)
map: 37 wallclock secs (35.93 usr + 0.55 sys = 36.48 CPU) @ 1.37/s (n=50)

#!/usr/bin/perl

use Benchmark;

our @arr = (1..100_000);

sub test1
{
my @a = @arr;
$_ .= "\n"
foreach @a;
}

sub test2
{
my @a = @arr;
for(my $i=0; $i<scalar @a; $i++)
{
$a[$i] .= "\n";
}
}

sub test3
{
my @a = @arr;
@a = map { $_."\n" } @a;
}

timethese(50, {
foreach => \&test1,
for_index => \&test2,
map => \&test3,
});

-----ScottG.
 
Reply With Quote
 
Suresh Govindachar
Guest
Posts: n/a
 
      08-13-2004

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Suresh Govindachar wrote:
> > What is a good way (with respect to speed)
> > to un-chomp an array?
> >
> > for(my $i=0; $i<scalar @lines; $i++)
> > {
> > $lines[$i] .= "\n";
> > }

>
> This is simpler code:
>
> print map "$_\n", @lines;


Doesn't the above code print? I actually want to
change the array @lines.

> As regards speed, I have no idea. If that is a concern of yours, do a
> benchmark ("perldoc Benchmark").


OK about doing benchmarks; would like to have candidate
codes to do the benchmark on.

--Suresh


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-13-2004
Suresh Govindachar wrote:
> Gunnar Hjalmarsson wrote:
>> Suresh Govindachar wrote:
>>> What is a good way (with respect to speed)
>>> to un-chomp an array?
>>>
>>> for(my $i=0; $i<scalar @lines; $i++)
>>> {
>>> $lines[$i] .= "\n";
>>> }

>>
>> This is simpler code:
>>
>> print map "$_\n", @lines;

>
> Doesn't the above code print? I actually want to
> change the array @lines.


Then do:

$_ .= "\n" for @lines;

Besides, according to Scott's benchmark, that's fastest as well.

However, I can't help to ask: Was it motivated to chomp the lines in
the first place? Did you for instance do something like this:

my @lines = <FILE>;
for (@lines) {
chomp;
...

??
In that case, maybe it would have been possible to keep the trailing
newline characters all through instead?

And another question: Was it motivated to load all the lines into
memory, or would it have been sufficient to process them one at a time:

while (<FILE>) {
...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      08-13-2004

"Suresh Govindachar" <(E-Mail Removed)> wrote in message
news:mf9Tc.7355$(E-Mail Removed)...
> Hello,
>
> What is a good way (with respect to speed)
> to un-chomp an array?
>
> for(my $i=0; $i<scalar @lines; $i++)


So many ugly C-style for loops lately:

for my $i (0..$#lines)

> {
> $lines[$i] .= "\n";
> }


You could question why you're adding newlines (i.e., can it wait until you
output the array, and do the alternatives at that stage -- map or join, for
example -- make a difference), but otherwise there's no complexity in the
loop you've posted to improve on.

Matt


 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      08-13-2004

"Scott W Gifford" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> "Suresh Govindachar" <(E-Mail Removed)> writes:
>
> > Hello,
> >
> > What is a good way (with respect to speed)
> > to un-chomp an array?

>
> This was the fastest way I could come up with:
>
> $_ .= "\n"
> foreach @lines;
>
> I benchmarked it, your original, and a map-based solution, and found
> it was about 30% faster than your original, and about twice as fast as
> map:
>
> Benchmark: timing 50 iterations of for_index, foreach, map...
> for_index: 18 wallclock secs (17.44 usr + 0.31 sys = 17.75 CPU) @

2.82/s (n=50)
> foreach: 13 wallclock secs (12.54 usr + 0.17 sys = 12.71 CPU) @

3.93/s (n=50)
> map: 37 wallclock secs (35.93 usr + 0.55 sys = 36.48 CPU) @

1.37/s (n=50)
>



for_index: 11 wallclock secs (11.44 usr + 0.02 sys = 11.45 CPU) @ 4.37/s
(n=50)
foreach: 10 wallclock secs (10.06 usr + 0.02 sys = 10.08 CPU) @ 4.96/s
(n=50)

Not sure why you're getting such a large discrepancy.

Matt


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-13-2004
Scott W Gifford <(E-Mail Removed)> wrote:
> "Suresh Govindachar" <(E-Mail Removed)> writes:



>> What is a good way (with respect to speed)
>> to un-chomp an array?

>
> This was the fastest way I could come up with:
>
> $_ .= "\n"
> foreach @lines;



If we want unchomp(), then we'd better:

$_ .= $/ foreach @lines;

so it will work even when the input record separator is not the default.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      08-14-2004
"Matt Garrish" <(E-Mail Removed)> wrote in
news:y8aTc.32778$(E-Mail Removed) :

>
> "Suresh Govindachar" <(E-Mail Removed)> wrote in message
> news:mf9Tc.7355$(E-Mail Removed)...
>> Hello,
>>
>> What is a good way (with respect to speed)
>> to un-chomp an array?
>>
>> for(my $i=0; $i<scalar @lines; $i++)

>
> So many ugly C-style for loops lately:
>
> for my $i (0..$#lines)


Still ugly

--
John Small Perl scripts:http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-14-2004
John Bokma <(E-Mail Removed)> wrote:
> "Matt Garrish" <(E-Mail Removed)> wrote in
> news:y8aTc.32778$(E-Mail Removed) :
>> "Suresh Govindachar" <(E-Mail Removed)> wrote in message
>> news:mf9Tc.7355$(E-Mail Removed)...


>>> What is a good way (with respect to speed)
>>> to un-chomp an array?
>>>
>>> for(my $i=0; $i<scalar @lines; $i++)

>>
>> So many ugly C-style for loops lately:
>>
>> for my $i (0..$#lines)

>
> Still ugly



But it has a great personality!


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a good way to change between int array and byte array ? Baby Lion Java 2 10-02-2006 01:45 AM
What's the best/a good way to access a variable member of an array of structs? sherifffruitfly@gmail.com C++ 5 04-24-2006 12:24 PM
What's the best/a good way to access a variable member of an array of structs? sherifffruitfly@gmail.com C++ 10 04-23-2006 04:34 AM
way way way OT: MCNGP Announcement Neil MCSE 174 04-17-2006 05:55 PM
AMD Opteron: 1-way, 2-way, ... Up to 8-way. John John Windows 64bit 12 12-27-2005 08:17 AM



Advertisments