在大型Perl6 CArray上执行高性能数学运算?

时间:2018-12-22 08:17:36

标签: perl6 nativecall

我有一些本机子返回的大型CArray,需要对其执行基本的逐元素数学运算。 CArray通常约为10 ^ 6个元素。我发现对它们调用.list将它们视为普通的Perl6类型非常昂贵。有没有办法对它们执行高性能的元素操作,同时又将它们保留为CArray?

简短的测试脚本可以计时一些我尝试过的方法:

Content-Type

输出:

#!/usr/bin/env perl6
use v6.c;
use NativeCall;
use Terminal::Spinners;

my $list;
my $carray;
my $spinner = Spinner.new;

########## create data stuctures ##########

print "Creating 10e6 element List and CArray  ";
my $create = Promise.start: {
    $list = 42e0 xx 10e6;
    $carray = CArray[num32].new($list);
}
$spinner.await: $create;

########## time List subtractions ##########

my $time = now;
print "Substracting two 10e6 element Lists w/ hyper  ";
$spinner.await( Promise.start: {$list >>-<< $list} );
say "List hyper subtraction took: {now - $time} seconds";

$time = now;
print "Substracting two 10e6 element Lists w/ for loop  ";
my $diff = Promise.start: {
    for ^$list.elems {
        $list[$_] - $list[$_];
    }
}
$spinner.await: $diff;
say "List for loop subtraction took: {now - $time} seconds";

########## time CArray subtractions ##########

$time = now;
print "Substracting two 10e6 element CArrays w/ .list and hyper  ";
$spinner.await( Promise.start: {$carray.list >>-<< $carray.list} );
say "CArray .list and hyper subtraction took: {now - $time} seconds";

$time = now;
print "Substracting two 10e6 element CArrays w/ for loop  ";
$diff = Promise.start: {
    for ^$carray.elems {
        $carray[$_] - $carray[$_];
    }
}
$spinner.await: $diff;
say "CArray for loop subtraction took: {now - $time} seconds";

for循环方法似乎最快,但是CArray的处理时间比List要长6倍。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

只要您可以使用其他本机数据类型(矩阵和向量),就可以使用Gnu Scientific Library by Fernando Santagata的(也是本机)端口。它具有可以使用的Vector.sub函数。

#!/usr/bin/env perl6
use v6.c;
use NativeCall;
use Terminal::Spinners;
use Math::Libgsl::Vector;

my $list;
my $carray;
my $spinner = Spinner.new;

########## create data stuctures ##########

print "Creating 10e6 element List and CArray  ";
my $list1 = Math::Libgsl::Vector.new(size => 10e6.Int);
$list1.setall(42);
my $list2 = Math::Libgsl::Vector.new(size => 10e6.Int);
$list2.setall(33);

########## time List subtractions ##########

my $time = now;
print "Substracting two 10e6 element Lists w/ hyper  ";
$spinner.await( Promise.start: { $list1.sub( $list2)} );
say "GSL Vector subtraction took: {now - $time} seconds";

这需要:

GSL Vector subtraction took: 0.08243 seconds

你够快吗? :-)