Fast high-level programming languages
Python and R are slow when they can’t rely on functionality or libraries backed by C/C++. They are inefficient not only for certain algorithm development but also for common tasks such as FASTQ parsing. Using these languages limits the reach of biologists. Sometimes you may have a brilliant idea but can’t deliver a fast implementation only because of the language in use. This can be frustrating. I have always been searching for a high-level language that is fast and easy to use by biologists. This blog post reports some of my exploration. It is inconclusive but might still interest you.
The source code and the full table are available at my lh3/biofast github repo. You can also found the machine setup, versions of libraries in use and some technical notes. I will only show part of the results here.
The following table shows the CPU time in seconds for parsing a gzip’d FASTQ (tgzip) or a plain FASTQ (tplain). We only count the number of sequences and compute the sum of lengths. Those seamlessly parsing multi-line FASTA/FASTQ all use an algorithm similar to my kseq.h parser in C.
|Language||Ext. Library||tgzip (s)||tplain (s)||Comments|
|Rust||needletail||9.3||0.8||multi-line fasta/mostly 4-line fastq|
|Go||19.1||2.8||4-line fastq only|
|Python||BioPython||37.9||18.1||multi-line fastq; FastqGeneralIterator|
|Python||BioPython||135.8||107.1||multi-line fastq; SeqIO.parse|
This benchmark stresses on I/O and string processing. I replaced the low-level I/O of several languages to achieve good performance. The code looks more like C than a high-level language, but at least these langauges give me the power without resorting to C.
It is worth mentioning the default BioPython FASTQ parser is over 70 times slower on plain FASTQ and over 10 times slower on gzip’d FASTQ. Running the C implementation on a human 30X gzip’d FASTQ takes 20 minutes. The default BioPython parser will take four and half hours, comparable to bwa-mem2 multi-thread mapping. If you want to parse FASTQ but doesn’t need other BioPython functionality, choose PyFastx or mappy.
Interval overlap query
The following table shows the CPU time in seconds for computing the breadth of coverage of a list intervals compared against another interval list. There are two columns for timing and memory footprint, depending on which list is loaded into memory.
|Language||tg2r (s)||Mg2r (Mb)||tr2g (s)||Mr2g (Mb)|
My take on fast high-level languages
The following is subjective and can be controversial, but I need to speak it out. Performance is not everything. Some subtle but important details are only apparent to those who write these programs.
These are two similar languages. They are old and were not designed with Just-In-Time (JIT) compilation in mind. People later developed JIT compilers and made them much faster. I like the two languages. They are easy to use, have few performance pitfalls and are pretty fast. Nonetheless, they are not the right languages for bioinformatics. If they were, they would have prevailed years ago.
Among the three more modern languages Julia, Nim and Crystal, Julia reached 1.0 first. I think Julia could be a decent replacement of Matlab or R by the language itself. If you like the experience of Matlab or R, you may like Julia. It has builtin matrix support, 1-based coordinate system, friendly REPL and an emphasis on plotting as well. I heard its differential equation solver might be the best across all languages.
I don’t see Julia a good replacement of Python. Julia has a long startup time. When you use a large package like Bio.jl, Julia may take 30 seconds to compile the code, longer than the actual running time of your scripts. You may not feel it is fast in practice. Actually in my benchmark, Julia is not really as fast as other languages, either. Probably my Julia implementations here will get most slaps. I have seen quite a few you-are-holding-the-phone-wrong type of responses from Julia supporters. Also importantly, the Julia developers do not value backward compatibility. There may be a python2-to-3 like transition in several years if they still hold their views by then. I wouldn’t take the risk.
Nim reached its maturity in September 2019. Its syntax is similar to python on the surface, which is a plus. It is relatively easier to get descent performance out of Nim. I have probably spent least time on learning Nim but I can write programs faster than in Julia.
Crystal is a pleasant surprise. On the second benchmark, I got a fast implementation on my first try. I did take a detour on FASTQ parsing when I initially tried to use Crystal’s builtin buffered reader, but again I got C-like performance immediately after I started to manage buffers by myself.
Crystal resembles Ruby a lot. It has very similar syntax, including a class/mixin system familiar to modern programmers. Some elementary tutorials on Ruby are even applicable to Crystal. I think building on top of successful languages is the right way to design a new language. Julia on the other hand feels different from most mainstream languages like C++ and Python. Some of its key features haven’t stood the test of time and may become frequent sources of bugs and performance traps.
To implement fast programs, we need to care about reference vs value. Crystal is no different. The good thing about Crystal is that reference and value are explicit with its class system. Among Julia, Nim and Crystal, I feel most comfortable with Crystal.
Crystal is not without problems. First, it is hard to install Crystal without the root permission. I am providing a portable installation binary package in lh3/PortableCrystal. It alleviates the issue for now. Second, Crystal is unstable. Each release introduces multiple breaking changes. Your code written today may not work later. Nonetheless, my program seems not affected by breaking changes in the past two years. This has given me some confidence. The Crystal devs also said 1.0 is coming “in the near future”. I will look forward to that.
A good high-level high-performance programming language would be a blessing to the field of bioinformatics. It could extend the reach of biologists, shorten the development time for experienced programmers and save the running time of numerous python scripts by many folds. However, no languages are good enough in my opinion. I will see how Crystal turns out. It has potentials.
Someone posted this blog post to Hacker New, Crystal subreddit, and Julia discourse. The reaction from many Julia supporters is just as I expected. That said, I owe a debt of gratitude to Kenta Sato for improving my Julia implementation. I geniunely appreciate.
Update on 2020-05-19: Added contributed Go implementations. More accurate timing for fast implementations, measured by hyperfine.
Update on 2020-05-20: Added a contributed Rust implementation. Added PyPy.
Update on 2020-05-21: Faster Nim and Julia with memchr. Faster Julia by adjusting three additional lines. For gzip’d input, Julia-1.4.1 is slow due to a misconfiguration on the Julia end. The numbers shown in the table are acquired by forcing Julia to use the system zlib on CentOS7. Added Python bedcov implementation. It is slow.
Update on 2020-05-23: Added a faster contributed Rust implementation.
blog comments powered by Disqus