SPVM::Document::Tutorial - SPVM Performance Tutorial
SPVM Tutorial
cpanm SPVM
cpan SPVM
# lib/SPVM/MyMath.spvm class MyMath { static method sum : int ($nums : int[]) { my $total = 0; for (my $i = 0; $i < @$nums; $i++) { $total += $nums->[$i]; } return $total; } }
# Class Definition class MyMath { }
class MyMath { # Method Definition static method sum : int ($nums : int[]) { } }
int
int[]
my $total = 0;
my $total : int = 0;
# Integer Literal 123 123_456_789 # Floating Point Literal 1.32 1.32f # Character Literal 'a' 'c' # String Literal "Hello World!"
for (my $i = 0; $i < @$nums; $i++) { $total += $nums->[$i]; }
@$nums
$i++
$nums->[$i]
$total += $nums->[$i];
$total = $total + $nums->[$i];
return $total;
use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'MyMath'; # Call method my $total = SPVM::MyMath->sum([3, 6, 8, 9]); print "Total: $total\n"; # Call method with packed data my $nums_packed = pack('l*', 3, 6, 8, 9); my $sv_nums = SPVM::new_int_array_from_bin($nums_packed); my $total_packed = SPVM::MyMath->sum($sv_nums); print "Total Packed: $total_packed\n";
use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib";
use SPVM 'MyMath';
# Call method my $total = SPVM::MyMath->sum([3, 6, 8, 9]);
# Call method with packed data my $nums_packed = pack('l*', 3, 6, 8, 9); my $sv_nums = SPVM::new_int_array_from_bin($nums_packed); my $total_packed = SPVM::MyMath->sum($sv_nums);
# spvm.pl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'MyMath'; # Call method my $total = SPVM::MyMath->sum([3, 6, 8, 9]); print "Total: $total\n"; # Call method with packed data my $nums_packed = pack('l*', 3, 6, 8, 9); my $sv_nums = SPVM::new_int_array_from_bin($nums_packed); my $total_packed = SPVM::MyMath->sum($sv_nums); print "Total Packed: $total_packed\n";
# lib/SPVM/MyMath.spvm class MyMath : precompile { static method sum_precompile : int ($nums : int[]) { my $total = 0; for (my $i = 0; $i < @$nums; $i++) { $total += $nums->[$i]; } return $total; } }
# spvm.pl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'MyMath'; # Call precompile method my $total_precompile = SPVM::MyMath->sum_precompile([3, 6, 8, 9]); print "Total Precompile: $total_precompile\n";
# lib/SPVM/MyMath.spvm class MyMath { native static method sum_native : int ($nums : int[]); } // lib/SPVM/MyMath.c #include "spvm_native.h" int32_t SPVM__SPVM__MyMath__sum_native(SPVM_ENV* env, SPVM_VALUE* stack) { void* sv_nums = stack[0].oval; int32_t length = env->length(env, sv_nums); int32_t* nums = env->get_elems_int(env, sv_nums); int32_t total = 0; for (int32_t i = 0; i < length; i++) { total += nums[i]; } stack[0].ival = total; return 0; } # lib/SPVM/MyMath.config use strict; use warnings; use SPVM::Builder::Config; my $config = SPVM::Builder::Config->new_c99; $config;
# spvm.pl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'MyMath'; # Call native method my $total_native = SPVM::MyMath->sum_native([3, 6, 8, 9]); print "Total Native: $total_native\n";
# bash example export SPVM_BUILD_DIR=~/.spvm_build
# bind_clib.pl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'BindCLib'; my $total = SPVM::BindCLib->sum([1, 2, 3, 4]); print "Total: $total\n";
# lib/SPVM/BindCLib.spvm class BindCLib { native static method sum : int ($nums : int[]); }
# lib/SPVM/BindCLib.config use strict; use warnings; use SPVM::Builder::Config; my $config = SPVM::Builder::Config->new_c99; $config;
// lib/SPVM/BindCLib.c #include "spvm_native.h" #include "bind_clib.h" int32_t SPVM__SPVM__BindCLib__sum(SPVM_ENV* env, SPVM_VALUE* stack) { void* sv_nums = stack[0].oval; int32_t length = env->length(env, sv_nums); int32_t* nums = env->get_elems_int(env, sv_nums); int32_t total = bind_clib_sum(nums, length); stack[0].ival = total; return 0; }
#include "bind_clib.h"
#include int32_t bind_clib_sum(int32_t* nums, int32_t length);
#include "bind_clib.h" int32_t bind_clib_sum(int32_t* nums, int32_t length) { int32_t total = 0; for (int32_t i = 0; i < length; i++) { total += nums[i]; } return total; }
To install SPVM, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install SPVM
For more information on module installation, please visit the detailed CPAN module installation guide.