Disadvantages of Purely Functional Programming Links
Total Page:16
File Type:pdf, Size:1020Kb
Mehr Nächster Blog» Blog erstellen Anmelden Diese Website verwendet Cookies von Google, um ihre Dienste bereitzustellen, Anzeigen zu personalisieren und Zugriffe zu analysieren. Informationen darüber, wie du die Website verwendest, werden an Google WEITERE INFORMATIONEN OK weitergegeben. Durch die Nutzung dieser Website erklärst du dich damit einverstanden, dass sie Cookies verwendet. The Flying Frog Blog Monday, 30 May 2016 Disadvantages of purely functional programming Links I have been asked to elaborate on my answer on Quora so here Visual F# 2010 for Technical Computing goes: (book) The F#.NET Journal (on-line magazine) The OCaml Journal (on-line magazine) F# for Numerics (library) 1. There is no efficient purely functional unsorted dictionary F# for Visualization (library) or set OCaml for Scientists (book) FFT .NET (library) Since the 1990s the use of dictionaries in software has gone Time-Frequency Analysis (Mathematica through the roof. Dictionaries are now a stock collection type that library) every programmer expects to find in their standard library. High-Level Virtual Machine (HLVM) Smoke Vector Graphics (OCaml library) Purely functional or persistent data structures such as those found in Okasaki’s fabulous monograph on the subject can be a great tool. In particular, the persistence they offer means you can reuse old versions of collections without having to worry about mutation. Popular Posts In many cases (particularly for some kinds of problems such as logic programming and compiler writing) this can make solutions Disadvantages of purely functional shorter and clearer, partly because it makes backtracking trivial. programming However, persistence comes at a great cost in terms of I have been asked to elaborate on my performance: purely functional dictionaries are typically 10x slower answer on Quora so here goes: 1. than a decent hash table and I have seen them run up to 40x There is no efficient purely functional unsorted dictionary o... slower. For some applications this is just too slow. Why is Haskell used so little in the Furthermore, most functional programming languages (OCaml, industry? Haskell, Scala) are incapable of expressing a fast generic mutable Bugspy asked an interesting question on hash table because they lack the killer combo of: reified generics, Stack Overflow recently: It is a wonderful, value types and a fast GC write barrier. very fast, mature and complete language. It exists fo... BEWARE: people who try to claim that Haskell’s purely functional dictionaries are fast by comparing them with Haskell’s mutable Are functional languages inherently slow? Functional languages require infrastructure hash tables. The correct conclusion is that Haskell’s mutable hash that inevitably adds overheads over what tables are slow. can theoretically be attained using assembler by hand.... F# vs OCaml vs Haskell: hash table 2. There is no purely functional weak hash table. performance I just stumbled upon an interesting With a garbage collected imperative language, the relationships benchmark. Put mappings i -> i for i in between the vertices and edges of a graph can be expressed using 1..10M in a hash table, print the entry for weak hash tables. The garbage collector will then collect 100. Don Stewart&... unreachable subgraphs for you. There is no purely functional weak Boost's shared_ptr up to hash table so, in a pure language, you must write your own 10× slower than OCaml's garbage collector. garbage collection Our recent post about the Note that this is a really fringe disadvantage with most developers merits of accurate garbage never having used a weak hash table! collection over reference counting prompted Tezka to ask for measurements demonstratin... 3. There are no purely functional concurrent collections. The rise and fall of OCaml After over 3 years, The By definition, immutable collections cannot support concurrent OCaml Journal is finally closing down. New mutation. Consequently, if you want a shared mutable collection subscriptions offer access to such as an in-memory database then there is no efficient purely the 75 existing articles and functional solution. only a few mo... The "C++ renaissance" According to Herb Sutter, 4. Most graph algorithms look worse and run much slower C++ (and C and Fortran) when written in an FP style. are unmatched for performance per dollar and, Purely functional programming is a great tool for some kinds of therefore, he believes C++ will once again t... problems but graph algorithms are one place where I have noticed that pure solutions are often worse both in terms of speed and Memory management myths: promptness clarity. People often assert that scope-based reference counting such as shared_ptr in Compare Prim’s algorithm in 12 lines of Python with Prim’s C++ collects garbage “promptly” and algorithm in 20 lines of Haskell. And why does the Haskell use some people define this... Prim’s algorithm? Probably because Kruskal’s algorithm is built upon the union-find collection and there is no known efficient Are multicore-capable garbage collectors hard to write? purely functional union-find collection. In this era of multicore computing, garbage collectors need to allow user threads (aka mutators ) to run in parallel on separate cores simul... 5. The inertia of traditional imperative data structures and algorithms is huge. Mono 2.2 vs OCaml vs .NET vs LLVM vs JDK Beyond graph algorithms, there are many parts of computer Mono 2.2 was recently science where 65 years of published literature has focused almost shipped and a new JIT code entirely upon imperative solutions. Consequently, imperative generator was the main selling point promoted by programmers can easily build upon the backs of giants whereas the Mono project because it provides the purely functional programmers are often left starting from scratch. ... After all, just a few years ago memoization in Haskell was the topic of a PhD thesis! I once challenged a group of Haskell programmers (several of Follow by Email whom had PhDs in Haskell) to write an efficient generic parallelised quicksort in Haskell and this is what happened. Email address... Submit 6. All existing implementations of functional programming Related Blogs languages, both pure and impure, happen to allocate far too much by design. F# News Fractal Explorer - The F# Journal just Around 1960, McCarthy invented Lisp. The core data structure was published an article about parsing: *"A the singly-linked list. Each list node was a separate heap allocated fractal is a mathematical set that exhibits a self-repeating structure at block. All modern functional languages evolved from this. In the arbitrary scales and s... 1970s, Scheme used essentially the same data representation 3 weeks ago strategy as Lisp. In the 1980s, SML added a little unboxing with tuples heap allocated as a single block of memory. In the 1990s, OCaml added a little more with unboxed float arrays. Haskell added Subscribe the ability to unbox some data. But to date no functional programming language has unboxed tuples by default. Even F#, Posts which sits on .NET which provides arbitrary value types, still uses .NET’s boxed tuples. Consequently, all modern functional Comments programming languages incur very high allocation rates for essentially no good reason. Consequently, they all stress their garbage collectors far more than necessary. This is a serious Blog Archive problem not just because it makes serial code slow but because the garbage collector is a shared resource and, therefore, stressing the ▼ 2016 (3) GC impedes the scalability of parallel programs. ▼ May (2) Disadvantages of purely functional I should note that calling this a “disadvantage” is contentious. programming Xavier Leroy of OCaml fame regards OCaml’s Lisp-like data ARM code generation quality representation as a good thing because it is the backbone of ► January (1) OCaml’s excellent performance when running the Coq theorem ► 2015 (5) prover. Here Xavier asserted that “OCaml's strategy is close to ► 2014 (1) optimal for symbolic computing”. Functional languages are often ► 2013 (10) optimised for high performance purely functional collections at the ► 2012 (23) expense of low performance imperative collections. Given that ► 2011 (16) imperative collections are generally faster, this puts an artificially- ► 2010 (53) low ceiling on the performance of almost all functional languages. ► 2009 (28) ► 2008 (10) ► 2007 (21) 7. Purely functional programming is theoretically good for parallelism but bad for performance in practice, which is the Labels sole purpose of parallelism. There are two reasons to write parallel programs today. The first is .net (5) to write objectively fast solutions. The second is to make a slow academic (1) solution less slow. In most cases, parallel programming in accumulator generator (1) functional languages is a form of the latter. Almost nobody in high advantages (1) performance computing circles (i.e. supercomputers) is running allocation (1) functional code directly. When most functional programmers amd (1) employ parallel programming today they do so not to attain the amorphous (1) analysis (1) best absolute performance but just to improve the performance animation (1) they have. API (1) Purely functional languages like Haskell are designed to abstract apple (2) away space and time. This gives you a higher-level perspective of arm (3) your solution but it makes it very hard to reason about the amount array (1) articles (1) of memory or length of time a Haskell program will require to asynchronous (2) produce a result. In parallel programming it is always important to asynchronous workflow (1) make sure that the gain from parallelisation outweighs the beginner (1) administrative overheads of running code in parallel. Haskell makes Beltway (1) this very hard. So hard, in fact, that published research on parallel benchmark (5) Haskell notoriously cherry picks the degree of parallelisation that bfg (1) maximises performance even though that degree could not be bitvector (1) predicted before running the program many times.