Perl benchmark

I recently read about a benchmark the compares the performances of lots of languages. The test consists in calculating the properties of an optical system by tracing some rays, a task that requires the use of trigonometric functions. Normalizing the results to C, it reports that Perl is about 23 times slower. This is not too surprising, as Perl pays in performance for being much more expressive and flexible, though those attributes are not taken advantage of in this number crunching example. Nevertheless, there is a system, the Perl Data Language, PDL (metacpan.org, Scientific computing with Perl) that extends Perl with objects that represent multidimensional arrays with which it is possible to do number crunching interfacing to fast C functions, and looping automatically over the dimensions of the arrays. Using Perl with PDL one retains the expressive power of Perl without sacrificing speed for number crunching code.

Thus I made a version version of the original perl code using PDL. It turns out my version runs about eight times faster than the original Perl program. On the other hand, mohawk2 made his own version using PDL and using its pp_def mechanism that allows writing C-like functions within the Perl code. His version is comparable in speed to C. Furthermore, simply by setting an environment variable, PDL_AUTOPTHREAD_TARG, the code runs in parallel threads and thus may become faster than C.

The results of my benchmark are summarized below.

Language # iters.  time (s)  speed (K/s)  factor
ansi C 150e6 133 1127.8195 1.
Perl 1.5e6 56 26.785714 42.1
PDL My version 15e6 67 223.88060 5.0
PDL mohawk2's version 15e6 16 937.5 1.2
mohawk2's version with 4 cores  15e6 11 1363.6364 0.8

Notice that in my system, Perl (5.32.1), Perl is 42 times slower than C, but with PDL it becomes only 5 times slower, and using pp_def it is comparable in speed and parallelizing it may even be faster.

Written on October 3, 2021