Here is the code...
这是代码......
use strict;
use warnings;
my @array= (1,2,3,4,5);
my $scalar= 5;
@array= $scalar*@array;
print @array;
Need something that can perform similar function with little code. Thanks!
需要能够用很少的代码执行类似功能的东西。谢谢!
5 个解决方案
#1
10
Use foreach.
使用foreach。
foreach my $x (@array) { $x = $x * $scalar; }
#2
9
You can try this:
你可以试试这个:
@array = map { $_ * $scalar } @array;
or more simply:
或更简单地说:
map { $_ *= $scalar } @array;
#3
7
Howabout this:
这个怎么样:
foreach(@array)
{ $_ *= $scalar }
As you see, you can modify the array in-place as it's traversed.
如您所见,您可以在遍历时就地修改数组。
#4
5
I don't know the scope of your need. IFF you are doing numerical data manipulation, the Perl Data Language (PDL) takes an array of numerical data, creates a "piddle" object from it and overloads mathematical operations to "vectorize" their operation. This is a very efficient system for doing numerical processing. Anyway here is an example:
我不知道你需要的范围。 IFF你正在进行数值数据操作,Perl数据语言(PDL)获取一组数值数据,从中创建一个“小提琴”对象,并重载数学运算以“向量化”它们的操作。这是一个非常有效的数字处理系统。无论如何这里是一个例子:
#!/usr/bin/perl
use strict;
use warnings;
use PDL;
my $pdl_array = pdl([1,1,2,3,5,8]);
print 2*$pdl_array;
__END__
gives:
[2 2 4 6 10 16]
#5
4
This comment is for SoloBold.
此评论适用于SoloBold。
Here is a test of the map
approach:
这是对地图方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
@array = map { $_ * $scalar } @array;
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is a test of the foreach
approach:
以下是foreach方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
foreach my $x (@array) { $x = $x * $scalar; }
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is the system I'm running on:
这是我正在运行的系统:
bash-3.2$ perl --version
This is perl, v5.8.8 built for darwin-2level
...
bash-3.2$ uname -a
Darwin Sounder.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
Here were results from one test:
以下是一项测试的结果:
bash-3.2$ ./test.map.pl
runtime: 4 wallclock secs ( 0.41 usr 0.70 sys + 0.00 cusr 0.00 csys = 1.11 CPU) sec
bash-3.2$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.13 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.13 CPU) sec
These times are fairly reproducible on the same machine, and the results are somewhat repeatable on a dual-core Linux box:
这些时间在同一台机器上相当可重复,并且在双核Linux机箱上的结果有些可重复:
[areynolds@fiddlehead ~]$ perl --version
This is perl, v5.8.8 built for x86_64-linux-thread-multi
...
[areynolds@fiddlehead ~]$ uname -a
Linux fiddlehead.example.com 2.6.18-194.17.1.el5 #1 SMP Mon Sep 20 07:12:06 EDT 2010 x86_64 GNU/Linux
[areynolds@fiddlehead ~]$ ./test.map.pl
runtime: 0 wallclock secs ( 0.28 usr 0.05 sys + 0.00 cusr 0.00 csys = 0.33 CPU) sec
[areynolds@fiddlehead ~]$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.09 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.09 CPU) sec
The ratio of performance on the OS X box is 8.53x slower for map
versus foreach
. On the Linux box, 3.67x slower for the same.
对于地图与foreach相比,OS X框上的性能比率要慢8.53倍。在Linux机器上,同样慢3.67倍。
My Linux box is dual-core and has a slightly faster cores than my single-core OS X laptop.
我的Linux机箱是双核的,核心比我的单核OS X笔记本电脑略快。
EDIT
编辑
I updated Perl from v5.8.8 to v5.12.3 on my OS X box and got a considerable speed boost, but map
still performed worse than foreach
:
我在我的OS X盒子上将Perl从v5.8.8更新到v5.12.3并获得了相当大的速度提升,但是地图仍然比foreach更糟糕:
sounder:~ alexreynolds$ perl --version
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-multi-2level
...
sounder:~ alexreynolds$ ./test.map.pl
runtime: 0 wallclock secs ( 0.45 usr 0.08 sys + 0.00 cusr 0.00 csys = 0.53 CPU) sec
sounder:~ alexreynolds$ ./test.foreach.pl
runtime: 1 wallclock secs ( 0.18 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.18 CPU) sec
This goes from 8.53x worse to 2.94x worse. A fairly substantial improvement.
这从8.53倍恶化到2.94倍恶化。相当可观的改进。
The Linux box performed slightly worse with upgrading its Perl installation to v5.12.2:
通过将其Perl安装升级到v5.12.2,Linux盒的性能稍差:
[areynolds@basquiat bin]$ perl --version
This is perl 5, version 12, subversion 2 (v5.12.2) built for x86_64-linux-thread-multi
...
[areynolds@basquiat bin]$ /home/areynolds/test.map.pl
runtime: 1 wallclock secs ( 0.29 usr 0.07 sys + 0.00 cusr 0.00 csys = 0.36 CPU) sec
[areynolds@basquiat bin]$ /home/areynolds/test.foreach.pl
runtime: 0 wallclock secs ( 0.08 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.08 CPU) sec
This goes from 3.67x worse to 4.5x worse — not so good! It might not always pay to upgrade, just for the heck of it.
这从3.67倍恶化到4.5倍恶化 - 不太好!升级可能并不总是值得的,只是为了它。
#1
10
Use foreach.
使用foreach。
foreach my $x (@array) { $x = $x * $scalar; }
#2
9
You can try this:
你可以试试这个:
@array = map { $_ * $scalar } @array;
or more simply:
或更简单地说:
map { $_ *= $scalar } @array;
#3
7
Howabout this:
这个怎么样:
foreach(@array)
{ $_ *= $scalar }
As you see, you can modify the array in-place as it's traversed.
如您所见,您可以在遍历时就地修改数组。
#4
5
I don't know the scope of your need. IFF you are doing numerical data manipulation, the Perl Data Language (PDL) takes an array of numerical data, creates a "piddle" object from it and overloads mathematical operations to "vectorize" their operation. This is a very efficient system for doing numerical processing. Anyway here is an example:
我不知道你需要的范围。 IFF你正在进行数值数据操作,Perl数据语言(PDL)获取一组数值数据,从中创建一个“小提琴”对象,并重载数学运算以“向量化”它们的操作。这是一个非常有效的数字处理系统。无论如何这里是一个例子:
#!/usr/bin/perl
use strict;
use warnings;
use PDL;
my $pdl_array = pdl([1,1,2,3,5,8]);
print 2*$pdl_array;
__END__
gives:
[2 2 4 6 10 16]
#5
4
This comment is for SoloBold.
此评论适用于SoloBold。
Here is a test of the map
approach:
这是对地图方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
@array = map { $_ * $scalar } @array;
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is a test of the foreach
approach:
以下是foreach方法的测试:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;
my $startTime = new Benchmark();
foreach my $x (@array) { $x = $x * $scalar; }
my $stopTime = new Benchmark();
print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";
Here is the system I'm running on:
这是我正在运行的系统:
bash-3.2$ perl --version
This is perl, v5.8.8 built for darwin-2level
...
bash-3.2$ uname -a
Darwin Sounder.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
Here were results from one test:
以下是一项测试的结果:
bash-3.2$ ./test.map.pl
runtime: 4 wallclock secs ( 0.41 usr 0.70 sys + 0.00 cusr 0.00 csys = 1.11 CPU) sec
bash-3.2$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.13 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.13 CPU) sec
These times are fairly reproducible on the same machine, and the results are somewhat repeatable on a dual-core Linux box:
这些时间在同一台机器上相当可重复,并且在双核Linux机箱上的结果有些可重复:
[areynolds@fiddlehead ~]$ perl --version
This is perl, v5.8.8 built for x86_64-linux-thread-multi
...
[areynolds@fiddlehead ~]$ uname -a
Linux fiddlehead.example.com 2.6.18-194.17.1.el5 #1 SMP Mon Sep 20 07:12:06 EDT 2010 x86_64 GNU/Linux
[areynolds@fiddlehead ~]$ ./test.map.pl
runtime: 0 wallclock secs ( 0.28 usr 0.05 sys + 0.00 cusr 0.00 csys = 0.33 CPU) sec
[areynolds@fiddlehead ~]$ ./test.foreach.pl
runtime: 0 wallclock secs ( 0.09 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.09 CPU) sec
The ratio of performance on the OS X box is 8.53x slower for map
versus foreach
. On the Linux box, 3.67x slower for the same.
对于地图与foreach相比,OS X框上的性能比率要慢8.53倍。在Linux机器上,同样慢3.67倍。
My Linux box is dual-core and has a slightly faster cores than my single-core OS X laptop.
我的Linux机箱是双核的,核心比我的单核OS X笔记本电脑略快。
EDIT
编辑
I updated Perl from v5.8.8 to v5.12.3 on my OS X box and got a considerable speed boost, but map
still performed worse than foreach
:
我在我的OS X盒子上将Perl从v5.8.8更新到v5.12.3并获得了相当大的速度提升,但是地图仍然比foreach更糟糕:
sounder:~ alexreynolds$ perl --version
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-multi-2level
...
sounder:~ alexreynolds$ ./test.map.pl
runtime: 0 wallclock secs ( 0.45 usr 0.08 sys + 0.00 cusr 0.00 csys = 0.53 CPU) sec
sounder:~ alexreynolds$ ./test.foreach.pl
runtime: 1 wallclock secs ( 0.18 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.18 CPU) sec
This goes from 8.53x worse to 2.94x worse. A fairly substantial improvement.
这从8.53倍恶化到2.94倍恶化。相当可观的改进。
The Linux box performed slightly worse with upgrading its Perl installation to v5.12.2:
通过将其Perl安装升级到v5.12.2,Linux盒的性能稍差:
[areynolds@basquiat bin]$ perl --version
This is perl 5, version 12, subversion 2 (v5.12.2) built for x86_64-linux-thread-multi
...
[areynolds@basquiat bin]$ /home/areynolds/test.map.pl
runtime: 1 wallclock secs ( 0.29 usr 0.07 sys + 0.00 cusr 0.00 csys = 0.36 CPU) sec
[areynolds@basquiat bin]$ /home/areynolds/test.foreach.pl
runtime: 0 wallclock secs ( 0.08 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.08 CPU) sec
This goes from 3.67x worse to 4.5x worse — not so good! It might not always pay to upgrade, just for the heck of it.
这从3.67倍恶化到4.5倍恶化 - 不太好!升级可能并不总是值得的,只是为了它。