http://www.perlmonks.org?node_id=11121970

in reply to Perl PDL slower than python numpy

Edit2. Here's PDL vs. numpy simple performance test in earnest, I hope Python code really does what I intended, and so no more blunders. I don't see any "numpy is better than PDL" as claimed, except PDL devs didn't bother to optimize "zeroes", as, it looks, it really fills every 8 bytes, cell by cell by cell... Too bad if initializing lots of arrays with zeroes is mission-critical. (That said, of course PDL isn't perfect.) Python3 and numpy are from 18.04LTS repositories.

```import time
import numpy as np
d = (1000,500,500)
t = time.time(); x = np.zeros(( d )); print( time.time() - t )
t = time.time(); y = np.ones(( d ));  print( time.time() - t )
t = time.time(); z = x / y;           print( time.time() - t )

1.1920928955078125e-05
0.6920394897460938
1.205686330795288

use strict;
use warnings;
use feature 'say';
use Time::HiRes 'time';
use PDL;
\$PDL::BIGPDL = \$PDL::BIGPDL = 1;
my @d = (1000,500,500);
my \$t;
\$t = time; my \$x = zeroes( @d ); say time - \$t;
\$t = time; my \$y = ones( @d );   say time - \$t;
\$t = time; my \$z = \$x / \$y;      say time - \$t;

0.727283954620361
0.730240821838379
0.971168994903564

-------

Edit. I was wrong about 'float32' being default for numpy, sorry. My answer doesn't explain the observed speed comparison.

-------

As quick googling shows, default data type for numpy is 32-bit "single precision" "float". PDL default is 64-bit "double precision" "double". Hence illusory 2x speed difference. Your example is just trivial allocation/arithmetic in underlying C, after all. In PDL, you can specify data type in constructor, as e.g. typing "?zeroes" in interactive shell will explain (and as shown below).

Complete cmd.exe window dump to show versions, etc.:

```----------------------------------------------
Welcome to Strawberry Perl PDL Edition!
* URL - http://strawberryperl.com + http://pdl.perl.org
* to launch perl script run:      perl c:\my\scripts\pdl-test.pl
* to start PDL console run:       pdl2
* to update PDL run:              cpanm PDL
* to install extra module run:    cpanm PDL::Any::Module
or if previous fails:    ppm PDL::Any::Module
* or you can use dev tools like:  gcc, g++, gfortran, gmake
----------------------------------------------
Perl executable: C:\berrybrew\strawberry-perl-5.30.1.1-64bit-PDL\perl\
+bin\perl.exe
Perl version   : 5.30.1 / MSWin32-x64-multi-thread
PDL version    : 2.019

C:\berrybrew\strawberry-perl-5.30.1.1-64bit-PDL>pdl2
Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call
+ didn't work. The COLUMNS and LINES environment variables didn't work
+. at C:/berrybrew/strawberry-perl-5.30.1.1-64bit-PDL/perl/vendor/lib/
+DL\data
Perldl2 Shell v0.008
PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file
'COPYING' in the PDL distribution. This is free software and you
are welcome to redistribute it under certain conditions, see
the same file for details.
CleanErrors
Commands
Completion
CompletionDriver::INC
CompletionDriver::Keywords
CompletionDriver::LexEnv
CompletionDriver::Methods
DDS
FindVariable
History
LexEnv
MultiLine::PPI
NiceSlice
PDLCommands
Packages
PrintControl
Type Ctrl-D or quit to exit
pdl>
pdl> \$PDL::BIGPDL = 1
pdl> use Time::HiRes 'time'; *t = \&time
pdl> @d=(500,500,500)    # 8 GB RAM here, let's avoid swapping
pdl>
pdl> \$t=t; \$x=zeroes@d; \$y=ones@d; \$z=\$x+\$y; \$z2=\$x/\$y; p t-\$t
1.2526330947876
pdl> \$t=t; \$x=zeroes@d; \$y=ones@d; \$z=\$x+\$y; \$z2=\$x/\$y; p t-\$t
1.69825196266174
pdl> \$t=t; \$x=zeroes@d; \$y=ones@d; \$z=\$x+\$y; \$z2=\$x/\$y; p t-\$t
1.59618711471558
pdl> p \$z2->info
PDL: Double D [500,500,500]
pdl>
pdl> \$t=t; \$x=zeroes double,@d; \$y=ones double,@d; \$z=\$x+\$y; \$z2=\$x/\$y
+; p t-\$t
1.64064288139343
pdl> \$t=t; \$x=zeroes double,@d; \$y=ones double,@d; \$z=\$x+\$y; \$z2=\$x/\$y
+; p t-\$t
1.6656858921051
pdl> \$t=t; \$x=zeroes double,@d; \$y=ones double,@d; \$z=\$x+\$y; \$z2=\$x/\$y
+; p t-\$t
1.68068408966064
pdl>
pdl> \$t=t; \$x=zeroes float,@d; \$y=ones float,@d; \$z=\$x+\$y; \$z2=\$x/\$y;
+p t-\$t
1.11372804641724
pdl> \$t=t; \$x=zeroes float,@d; \$y=ones float,@d; \$z=\$x+\$y; \$z2=\$x/\$y;
+p t-\$t
0.841649055480957
pdl> \$t=t; \$x=zeroes float,@d; \$y=ones float,@d; \$z=\$x+\$y; \$z2=\$x/\$y;
+p t-\$t
0.83014702796936
pdl> p \$z2->info
PDL: Float D [500,500,500]
pdl>