5 ms·
How many lines would the equivalent python program take?
by veyron 15y ago
How many lines would the equivalent python program take?
- tgrisfal 15y agoFive; one to import the library and four for frivolous comments.
- kkowalczyk 15y agoNow that upskirt has been ported to Go as a library, it's also 5 lines. BTW: if people want to do markdown in Go, the library to use is https://github.com/russross/blackfriday https://github.com/russross/blackfriday. It's a different one that the code I ported but Russ Ross did exactly the same thing at almost exactly the same time and was a little bit ahead, so after I discovered his work, I decided to contribute to his code instead of maintaining my own, almost identical, project.
- kkowalczyk 15y ago(I'm the author of the article and also a heavy Python programmer). That's not an easy question to answer. If you were to do a faithful port i.e. using using the same techniques as C/Go code, it would be very close. upskirt uses a traditional lexing/top-down-parsing approach. There's nothing in Python syntax that makes writing such code more compact than in Go. It's a bit tedious to write but the benefit is that (in C/Go) it gives the best speed because it minimizes the number of times each source character is looked at. If you were to use a different approach e.g. brute-forcing your way through the text several times with regexpes, which is the most popular way of doing that in dynamic languages, the code clocks at ~2 thousand lines (like the implementation I use for my home-grown blog system https://github.com/kjk/web-blog/blob/master/markdown2.py https://github.com/kjk/web-blog/blob/master/markdown2.py) If you were to use this technique in Go, the code would probably end up smaller than the manual lexing/parsing approach used in upskirt, but it would be significantly slower. Interestingly, upskirt approach would probably be slower in Python than the regexp approach, because regexpes are heavily optimized C code and looking at individual characters of the string in Python code isn't particularly fast due to python interpretation overhead.
- knome 15y ago> because regexpes are heavily optimized C code AFAIK, the python re module does not have a C language component. It is a pure python regular expression implementation.
- kkowalczyk 15y agoThat's not true: http://hg.python.org/cpython/file/d1d5a7392e39/Modules/_sre.c http://hg.python.org/cpython/file/d1d5a7392e39/Modules/_sre....
- deleted 15y ago[deleted]
- enneff 15y agoMost dynamic languages use a C Regular Expression library. That's why they can do so well on Regular Expression benchmarks. V8 is a notable exception. They use their code generation pipeline to JIT compile regexps to machine code, making it the fastest regexp engine around bar none (I think).
- azakai 15y agoAFAIK all modern JS engines JIT regexps: V8, SpiderMonkey and Nitro.
- silentbicycle 15y agoWhat about LPEG? It has a superset of the functionality of most regexp engines, yet is able to handle many things linearly that they would handle in quadratic time.
- pavpanchekha 15y agoI'd like to put in a word for CL-PPCRE, the Common Lisp regex library. It does the same except using the CL optimizing compiler, so also achieves amazing speed. Libraries don't appear in the Comp.Lang.Benchmark Shootout, so CL doesn't do so well.
- 15y ago