| Andrew Cooke | Contents | Latest | RSS | Twitter | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Lepl parser for Python.

Colorless Green.

Photography around Santiago.

SVG experiment.

Professional Portfolio

Calibration of seismometers.

Data access via web services.

Cache rewrite.

Extending OpenSSH.

C-ORM: docs, API.

Last 100 entries

Tomato Chutney v4; Have to add...; Culturally Liberal and Nothing More; Weird Finite / Infinite Result; Your diamond is a beaten up mess; Maths Books; Good Bike Route from Providencia / Las Condes to Panul\; Iain Pears (Author of Complex Plots); Plum Jam; Excellent; More Recently; For a moment I forgot StackOverflow sucked; A Few Weeks On...; Chilean Book Recommendations; How To Write Shared Libraries; Jenny Erpenbeck (Author); Dijkstra, Coins, Tables; Python libraries error on OpenSuse; Deserving Trump; And Smugness; McCloskey Economics Trilogy; cmocka - Mocks for C; Concept Creep (Americans); Futhark - OpenCL Language; Moved / Gone; Fan and USB issues; Burgers in Santiago; The Origin of Icosahedral Symmetry in Viruses; autoenum on PyPI; Jars Explains; Tomato Chutney v3; REST; US Elections and Gender: 24 Point Swing; PPPoE on OpenSuse Leap 42.1; SuperMicro X10SDV-TLN4F/F with Opensuse Leap 42.1; Big Data AI Could Be Very Bad Indeed....; Cornering; Postcapitalism (Paul Mason); Black Science Fiction; Git is not a CDN; Mining of Massive Data Sets; Rachel Kaadzi Ghansah; How great republics meet their end; Raspberry, Strawberry and Banana Jam; Interesting Dead Areas of Math; Later Taste; For Sale; Death By Bean; It's Good!; Tomato Chutney v2; Time ATAC MX 2 Pedals - First Impressions; Online Chilean Crafts; Intellectual Variety; Taste + Texture; Time Invariance and Gauge Symmetry; Jodorowsky; Tomato Chutney; Analysis of Support for Trump; Indian SF; TP-Link TL-WR841N DNS TCP Bug; TP-Link TL-WR841N as Wireless Bridge; Sending Email On Time; Maybe run a command; Sterile Neutrinos; Strawberry and Banana Jam; The Best Of All Possible Worlds; Kenzaburo Oe: The Changeling; Peach Jam; Taste Test; Strawberry and Raspberry Jam; flac to mp3 on OpenSuse 42.1; Also, Sebald; Kenzaburo Oe Interview; Otake (Kitani Minoru) move Black 121; Is free speech in British universities under threat?; I am actually good at computers; Was This Mansplaining?; WebFaction / LetsEncrypt / General Disappointment; Sensible Philosophy of Science; George Ellis; Misplaced Intuition and Online Communities; More Reading About Japan; Visibilty / Public Comments / Domestic Violence; Ferias de Santiago; More (Clearly Deliberate); Deleted Obit Post; And then a 50 yo male posts this...; We Have Both Kinds Of Contributors; Free Springer Books; Books on Religion; Books on Linguistics; Palestinan Electronica; Books In Anthropology; Taylor Expansions of Spacetime; Info on Juniper; Efficient Stream Processing; The Moral Character of Crypto; Hearing Aid Info; Small Success With Go!; Re: Quick message - This link is broken; Adding Reverb To The Echo Chamber

© 2006-2015 Andrew Cooke (site) / post authors (content).

RXPY Benchmarks

From: andrew cooke <andrew@...>

Date: Sat, 17 Jul 2010 16:48:30 -0400

Much of this is just duplicating the re2 work, and the rest is RXPY-specific,
but even so, these are quite interesting (for me, at least).

Note that the times are relative to the Python re package (log10 in brackets).
Also, the plots are both linear ### and logarithmic [ ].


First, some basic matching.  This is just showing how slow my code is
(hundreds of times slower than the re package).

Match a(.)c against abc
             Backtracking [##########                   ]              95 [2.0]
            Parallel wide [#######################            ]       206 [2.3]
    Parallel wide, hashed [#################################### ]     315 [2.5]
             Parallel seq [#######################            ]       205 [2.3]
     Parallel seq, hashed [###################################  ]     304 [2.5]
            Parallel beam [############################        ]      245 [2.4]
    Parallel beam, hashed [######################################]    339 [2.5]

Match (a)b(?<=(?(1)b|x))(c) against abc
             Backtracking [############                   ]           210 [2.3]
            Parallel wide [#########################          ]       398 [2.6]
    Parallel wide, hashed [#################################    ]     531 [2.7]
             Parallel seq [#########################          ]       400 [2.6]
     Parallel seq, hashed [##################################   ]     546 [2.7]
            Parallel beam [##############################      ]      480 [2.7]
    Parallel beam, hashed [######################################]    622 [2.8]

Match a*b against a^100b
             Backtracking [####################################  ]   1902 [3.3]
            Parallel wide [#################################    ]    1737 [3.2]
    Parallel wide, hashed [####################################  ]   1919 [3.3]
             Parallel seq [################################     ]    1733 [3.2]
     Parallel seq, hashed [####################################  ]   1919 [3.3]
            Parallel beam [###################################  ]    1884 [3.3]
    Parallel beam, hashed [######################################]   2073 [3.3]

Match .*b against a^100b
             Backtracking [#################                 ]       1902 [3.3]
            Parallel wide [###############                   ]       1769 [3.2]
    Parallel wide, hashed [#################                 ]       1927 [3.3]
             Parallel seq [###############                   ]       1757 [3.2]
     Parallel seq, hashed [##################                ]       2015 [3.3]
            Parallel beam [###################################  ]    3825 [3.6]
    Parallel beam, hashed [######################################]   4300 [3.6]

Above is quite interesting because the beam search is suddenly twice as slow,
just by changing from "a*b" to ".*b".  That's because the second expression
has a failure ("." matchs the final "b") and so needs to backtrack.  Because
the initial beam width is 1 that means that the entire search must be repeated
with doubled with.


The next test is a search.

Search .*b against a^100b
             Backtracking [                         ]                1172 [3.1]
            Parallel wide [######################################]  41841 [4.6]
    Parallel wide, hashed [                         ]                1459 [3.2]
             Parallel seq [                         ]                1212 [3.1]
     Parallel seq, hashed [                         ]                1338 [3.1]
            Parallel beam [###                          ]            3764 [3.6]
    Parallel beam, hashed [###                          ]            3688 [3.6]

Above shows the problem with a naive wide parallel approach - there are an
awful lot of different states to store.  Of course, most are duplicates so the
hashing removes the problem (as does sequential or beam search).


The next tests show the exponential explosion problem in Python's matcher.
This is a little more difficult to trigger than in the re2 paper because
Python has a "shortcut" (hack!) that avoids the problem in very simple cases
(but, as we will see below, does nothing to address the underlying issues).

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#############                   ]          573 [2.8]
            Parallel wide [#########                     ]            392 [2.6]
    Parallel wide, hashed [##                      ]                  125 [2.1]
             Parallel seq [#########                     ]            398 [2.6]
     Parallel seq, hashed [##                      ]                  125 [2.1]
            Parallel beam [######################################]   1606 [3.2]
    Parallel beam, hashed [########                      ]            360 [2.6]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [######################################]    637 [2.8]
            Parallel wide [#############################       ]      476 [2.7]
    Parallel wide, hashed [#                 ]                         27 [1.4]
             Parallel seq [#############################       ]      475 [2.7]
     Parallel seq, hashed [#                 ]                         27 [1.4]
    Parallel beam, hashed [##                    ]                     51 [1.7]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=8
    Parallel wide, hashed [##############       ]                       4 [0.6]
     Parallel seq, hashed [##############       ]                       4 [0.6]
    Parallel beam, hashed [######################################]     10 [1.0]

As the number of terms increase, only the hashed approaches are efficient (due
to the explosion in the number of states).  This is as expected.  What is nice
to see is that by n=8 my Python code is oly 4x slower than the re package!
This is because the re backtracker is "exploding", while my code is linear
(but slow).


This is only possible, though, when we can discard duplicate state.  Which
isn't so easy when matching groups:

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#######                       ]            481 [2.7]
            Parallel wide [#####                       ]              378 [2.6]
    Parallel wide, hashed [#########                      ]           627 [2.8]
             Parallel seq [######                       ]             414 [2.6]
     Parallel seq, hashed [##########                     ]           667 [2.8]
            Parallel beam [##########################          ]     1612 [3.2]
    Parallel beam, hashed [######################################]   2415 [3.4]

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [###########################         ]      512 [2.7]
            Parallel wide [######################             ]       415 [2.6]
    Parallel wide, hashed [######################################]    723 [2.9]
             Parallel seq [######################             ]       418 [2.6]
     Parallel seq, hashed [######################################]    714 [2.9]


And here we do no better than normal backtracking.


The main conclusions, then, are:

1 - Python's re package does suffer from the exponential "explosion" issue,
    while my code (with duplicate state elimination) does not.

2 - My code can also handle groups, but the degrades in performance as
    expected.

So RXPY is both general and, when possible, efficient.  Even if it is terribly
slow.

A secondary conclusion is that the beam search approach does not seem to be
worth the trouble.

Andrew

Future Work

From: andrew cooke <andrew@...>

Date: Sun, 18 Jul 2010 08:53:28 -0400

The following optimisations are needed at some point:

- Branches should test whether there is sufficient remaining text.  This can
  be precomputed by nodes.

- Search should identify (at least) initial sequences and optimise on those.
  See the existing re library code.  RXPY could go better and use non-startign
  sequences with a known lookback to start.

But for now I think I am going to produce a simplified version of the hashed,
wide engine and see how that can work on PyPy.

Andrew

More Benchmarks

From: andrew cooke <andrew@...>

Date: Sun, 25 Jul 2010 11:42:01 -0400

There are some new engines here.  "Simple" is a stripped down engine that does
not support loops or groups.  "Complex" is similar to "Wide hashed", but has
much more care taken in the implementation to reduce overheads.  Finally,
"Quick" uses "Simple" until failure, then changes to "Complex" - this is the
"final" engine.

Also, the "primality" tests are based on '^1?$|^(11+?)\1+$', which repeats
an increasingly large group until match.

Note that for "simple" tests, the overhead of "Quick" is significant.


Match . against a
             Backtracking [#############                 ]             49 [1.7]
            Parallel wide [#######################           ]         82 [1.9]
    Parallel wide, hashed [############################       ]        97 [2.0]
             Parallel seq [########################          ]         84 [1.9]
     Parallel seq, hashed [##########################         ]        92 [2.0]
            Parallel beam [#################################    ]     114 [2.1]
    Parallel beam, hashed [###################################  ]     122 [2.1]
          Parallel simple [##################################   ]     117 [2.1]
         Parallel complex [######################################]    135 [2.1]
                    Quick [##################################   ]     120 [2.1]

Match a(.)c against abc
             Backtracking [####                     ]                  60 [1.8]
            Parallel wide [#########                    ]             124 [2.1]
    Parallel wide, hashed [###############                 ]          193 [2.3]
             Parallel seq [##########                   ]             126 [2.1]
     Parallel seq, hashed [################                ]          201 [2.3]
            Parallel beam [############                   ]           159 [2.2]
    Parallel beam, hashed [#################                ]         214 [2.3]
         Parallel complex [###################               ]        240 [2.4]
                    Quick [######################################]    473 [2.7]

Match a*b against a^100b
             Backtracking [######################################]   1605 [3.2]
            Parallel wide [################################     ]    1384 [3.1]
    Parallel wide, hashed [####################################  ]   1519 [3.2]
             Parallel seq [################################     ]    1385 [3.1]
     Parallel seq, hashed [####################################  ]   1517 [3.2]
            Parallel beam [###################################  ]    1513 [3.2]
    Parallel beam, hashed [######################################]   1662 [3.2]
          Parallel simple [##############                   ]         630 [2.8]
         Parallel complex [############################        ]     1202 [3.1]
                    Quick [##############                   ]         624 [2.8]

Match .*b against a^100b
             Backtracking [##################                ]       1461 [3.2]
            Parallel wide [###############                   ]       1273 [3.1]
    Parallel wide, hashed [##################                ]       1444 [3.2]
             Parallel seq [################                  ]       1295 [3.1]
     Parallel seq, hashed [#################                 ]       1401 [3.1]
            Parallel beam [####################################  ]   2896 [3.5]
    Parallel beam, hashed [######################################]   3108 [3.5]
          Parallel simple [######                        ]            581 [2.8]
         Parallel complex [#############                    ]        1100 [3.0]
                    Quick [#######                       ]            585 [2.8]

Search .*b against a^100b
             Backtracking [                        ]                  846 [2.9]
            Parallel wide [######################################]  29364 [4.5]
    Parallel wide, hashed [                         ]                1011 [3.0]
             Parallel seq [                        ]                  730 [2.9]
     Parallel seq, hashed [                        ]                  818 [2.9]
            Parallel beam [##                          ]             2462 [3.4]
    Parallel beam, hashed [##                          ]             2110 [3.3]
          Parallel simple [                     ]                     335 [2.5]
         Parallel complex [                       ]                   690 [2.8]
                    Quick [                     ]                     335 [2.5]

Match (.*) (.*) (.*)  against abc abc abc
             Backtracking [#################                 ]        601 [2.8]
            Parallel wide [#############                   ]          465 [2.7]
    Parallel wide, hashed [#################                 ]        592 [2.8]
             Parallel seq [#############                   ]          450 [2.7]
     Parallel seq, hashed [################                 ]         568 [2.8]
            Parallel beam [################################     ]    1087 [3.0]
    Parallel beam, hashed [######################################]   1328 [3.1]
         Parallel complex [#############                   ]          471 [2.7]
                    Quick [####################              ]        706 [2.8]


Search .*?a*b against a^10b
             Backtracking [#                      ]                   109 [2.0]
            Parallel wide [######################################]   1991 [3.3]
    Parallel wide, hashed [####                       ]               242 [2.4]
             Parallel seq [########                      ]            441 [2.6]
     Parallel seq, hashed [###                       ]                222 [2.3]
            Parallel beam [#                      ]                   118 [2.1]
    Parallel beam, hashed [##                      ]                  129 [2.1]
          Parallel simple [#                     ]                    101 [2.0]
         Parallel complex [###                       ]                200 [2.3]
                    Quick [#                     ]                    101 [2.0]

Search .*?a*b against a^100b
             Backtracking [                         ]                1026 [3.0]
    Parallel wide, hashed [##                          ]             2542 [3.4]
             Parallel seq [######################################]  33381 [4.5]
     Parallel seq, hashed [##                          ]             2264 [3.4]
            Parallel beam [                         ]                1055 [3.0]
    Parallel beam, hashed [                         ]                1162 [3.1]
          Parallel simple [                        ]                  781 [2.9]
         Parallel complex [#                          ]              1922 [3.3]
                    Quick [                        ]                  780 [2.9]

Search .*?a*b against a^1000b
             Backtracking [##############                    ]       8137 [3.9]
    Parallel wide, hashed [######################################]  21470 [4.3]
     Parallel seq, hashed [###################################   ]  19550 [4.3]
            Parallel beam [################                   ]      9044 [4.0]
    Parallel beam, hashed [#################                  ]      9792 [4.0]
          Parallel simple [###########                      ]        6512 [3.8]
         Parallel complex [#############################        ]   16108 [4.2]
                    Quick [###########                      ]        6679 [3.8]


Match (a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [#######                       ]            492 [2.7]
            Parallel wide [#####                       ]              358 [2.6]
    Parallel wide, hashed [##########                     ]           615 [2.8]
             Parallel seq [#####                       ]              354 [2.5]
     Parallel seq, hashed [#########                      ]           597 [2.8]
            Parallel beam [########################            ]     1485 [3.2]
    Parallel beam, hashed [######################################]   2343 [3.4]
         Parallel complex [###                        ]               261 [2.4]
                    Quick [####                        ]              310 [2.5]

Match (a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [############################        ]      514 [2.7]
            Parallel wide [#####################             ]        401 [2.6]
    Parallel wide, hashed [######################################]    712 [2.9]
             Parallel seq [#####################             ]        396 [2.6]
     Parallel seq, hashed [######################################]    718 [2.9]
         Parallel complex [#################                ]         331 [2.5]
                    Quick [##################               ]         338 [2.5]


Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=4
             Backtracking [##############                   ]         597 [2.8]
            Parallel wide [#########                     ]            394 [2.6]
    Parallel wide, hashed [##                      ]                  127 [2.1]
             Parallel seq [#########                     ]            386 [2.6]
     Parallel seq, hashed [##                       ]                 141 [2.2]
            Parallel beam [######################################]   1619 [3.2]
    Parallel beam, hashed [########                      ]            366 [2.6]
          Parallel simple [                    ]                       58 [1.8]
         Parallel complex [##                      ]                  120 [2.1]
                    Quick [                    ]                       55 [1.7]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=6
             Backtracking [######################################]    628 [2.8]
            Parallel wide [############################        ]      457 [2.7]
    Parallel wide, hashed [#                 ]                         26 [1.4]
             Parallel seq [############################        ]      453 [2.7]
     Parallel seq, hashed [#                 ]                         26 [1.4]
    Parallel beam, hashed [##                    ]                     50 [1.7]
          Parallel simple [            ]                               10 [1.0]
         Parallel complex [                 ]                          23 [1.4]
                    Quick [            ]                               10 [1.0]

Match (?:a|b)?^2n(?:ab)^n against (ab)^n for n=8
    Parallel wide, hashed [##############       ]                       4 [0.6]
     Parallel seq, hashed [##############       ]                       4 [0.6]
    Parallel beam, hashed [######################################]     10 [1.0]
          Parallel simple [##]#                                         1 [0.1]
         Parallel complex [############      ]                          3 [0.5]
                    Quick [#]##                                         1 [0.1]

Match a?^na^n against a^n for n=8
    Parallel wide, hashed [##############                 ]           136 [2.1]
     Parallel seq, hashed [##############                 ]           132 [2.1]
    Parallel beam, hashed [######################################]    361 [2.6]
          Parallel simple [####                    ]                   49 [1.7]
         Parallel complex [###########                   ]            105 [2.0]
                    Quick [#####                    ]                  51 [1.7]


Primality 32
             Backtracking [#                      ]                   146 [2.2]
            Parallel wide [###########################         ]     2157 [3.3]
    Parallel wide, hashed [######################################]   3115 [3.5]
             Parallel seq [###########################         ]     2143 [3.3]
     Parallel seq, hashed [##################################### ]   2994 [3.5]
            Parallel beam [######                       ]             516 [2.7]
    Parallel beam, hashed [########                      ]            672 [2.8]
         Parallel complex [########                      ]            683 [2.8]
                    Quick [#########                      ]           754 [2.9]

Primality 37
             Backtracking [##                        ]                444 [2.6]
            Parallel wide [#######                        ]          1305 [3.1]
    Parallel wide, hashed [##########                      ]         1852 [3.3]
             Parallel seq [#######                        ]          1302 [3.1]
     Parallel seq, hashed [##########                      ]         1811 [3.3]
            Parallel beam [#############################        ]    4956 [3.7]
    Parallel beam, hashed [######################################]   6709 [3.8]
         Parallel complex [#                        ]                 378 [2.6]
                    Quick [#                        ]                 409 [2.6]

Primality 128
             Backtracking [                     ]                     401 [2.6]
            Parallel wide [#############################        ]   21910 [4.3]
    Parallel wide, hashed [######################################]  28511 [4.5]
             Parallel seq [############################         ]   21262 [4.3]
     Parallel seq, hashed [######################################]  29158 [4.5]
         Parallel complex [####                          ]           3876 [3.6]
                    Quick [####                          ]           3910 [3.6]

Primality 131
             Backtracking [####                        ]              563 [2.8]
            Parallel wide [#############################        ]    3298 [3.5]
    Parallel wide, hashed [######################################]   4397 [3.6]
             Parallel seq [############################        ]     3220 [3.5]
     Parallel seq, hashed [######################################]   4466 [3.6]
         Parallel complex [####                        ]              585 [2.8]
                    Quick [####                        ]              586 [2.8]

Primality (eager) 32
             Backtracking [                       ]                   313 [2.5]
            Parallel wide [########                        ]         2224 [3.3]
    Parallel wide, hashed [############                     ]        3068 [3.5]
             Parallel seq [########                        ]         2198 [3.3]
     Parallel seq, hashed [###########                      ]        2965 [3.5]
            Parallel beam [##############################       ]    7542 [3.9]
    Parallel beam, hashed [######################################]   9658 [4.0]
         Parallel complex [##                        ]                677 [2.8]
                    Quick [##                         ]               749 [2.9]

Primality (eager) 37
             Backtracking [##                        ]                534 [2.7]
            Parallel wide [######                        ]           1387 [3.1]
    Parallel wide, hashed [#########                       ]         1882 [3.3]
             Parallel seq [######                        ]           1370 [3.1]
     Parallel seq, hashed [#########                       ]         1854 [3.3]
            Parallel beam [##############################       ]    5855 [3.8]
    Parallel beam, hashed [######################################]   7586 [3.9]
         Parallel complex [#                        ]                 396 [2.6]
                    Quick [#                        ]                 436 [2.6]

Primality (eager) 128
             Backtracking [                       ]                   351 [2.5]
            Parallel wide [#############################        ]    8833 [3.9]
    Parallel wide, hashed [######################################]  11640 [4.1]
             Parallel seq [#############################        ]    8685 [3.9]
     Parallel seq, hashed [######################################]  11709 [4.1]
         Parallel complex [####                         ]            1590 [3.2]
                    Quick [####                          ]           1600 [3.2]

Primality (eager) 131
             Backtracking [####                         ]             631 [2.8]
            Parallel wide [###########################         ]     3310 [3.5]
    Parallel wide, hashed [##################################### ]   4419 [3.6]
             Parallel seq [###########################         ]     3244 [3.5]
     Parallel seq, hashed [######################################]   4692 [3.7]
         Parallel complex [####                        ]              586 [2.8]
                    Quick [####                        ]              596 [2.8]

Comment on this post