Theme:
v
APL+Win New Catenate Syntax Speedups

Not only is the new Catenate syntax nicer and more concise to use for the developer, but it can also considerably speed up your programs!

The benchmarks displayed below show that the new Catenate syntax is from 46 times to 134 times faster than the previous syntax!

Here are a few benchmarks measured with my Times APL function which returns milliseconds:

    ∇ R←TestCatenate1 A;I
[1]   R←⍬
[2]   :for I :in ⍳A
[3]       R←R,I
[4]   :endfor
    ∇

      2 Times'aaa←TestCatenate1 100000'
1799

and now using the new Catenate syntax:

        ∇ R←TestCatenate2 A;I
[1]   R←⍬
[2]   :for I :in ⍳A
[3]       R,←I    ⍝ note the new catenate syntax: R,←I
[4]   :endfor
    ∇

      2 Times'aaa←TestCatenate2 100000'
39
      aaa≡bbb
1

So the new syntax speeds up the TestCatenate program by a factor of 46!

This is not only true of catenating integers. Let's catenate strings instead:

    ∇ R←TestCatenate3 A;I
[1]   R←''
[2]   :for I :in ⍳A
[3]       R←R,I⊃strings
[4]   :endfor
    ∇

    ∇ R←TestCatenate4 A;I
[1]   R←''
[2]   :for I :in ⍳A
[3]       R,←I⊃strings   ⍝ note the new catenate syntax: R,←
[4]   :endfor
    ∇

      strings←'A',¨⍕¨⍳100000

      2 Times'aaa←TestCatenate3 100000'
5819
      2 Times'aaa←TestCatenate4 100000'
53.5

In the above case, the new Catenate syntax is 108.8 times faster than before!

Comparable speedups are obtained for nested vector of strings:

     ∇ R←TestCatenate5 A;I
[1]   R←''
[2]   :for I :in ⍳A
[3]       R←R,strings[I]
[4]   :endfor
    ∇

     ∇ R←TestCatenate6 A;I
[1]   R←''
[2]   :for I :in ⍳A
[3]       R,←strings[I]   ⍝ note the new catenate syntax: R,←
[4]   :endfor
    ∇

      strings←'A',¨⍕¨⍳100000

      2 Times'aaa←TestCatenate5 20000'
2017
      2 Times'bbb←TestCatenate6 20000'
15
      aaa≡bbb
1

So this time the new Catenate syntax is 134 times faster!

We can also test the case where we are catenating elements of an heterogeneous nested array, using the same TestCatenate5 and TestCatenate6 APL functions:

      strings←,('A',¨⍕¨⍳10000),[1.5]10000⍴⊂2 2⍴⍳4

      10↑strings
 A1    1 2   A2    1 2   A3    1 2   A4    1 2   A5    1 2
       3 4         3 4         3 4         3 4         3 4

      2 Times'aaa←TestCatenate5 20000'
1887
      2 Times'bbb←TestCatenate6 20000'
16
      aaa≡bbb
1

This time the speedup factor is 118!