爲什麼 Haskell (GHC) 這麼快? - Why is Haskell (GHC) so darn fast?

問題:

Haskell (with the GHC compiler) is a lot faster than you'd expect . Haskell(使用GHC編譯器) 比您預期的要快得多 Used correctly, it can get close-ish to low-level languages.使用得當,它可以接近低級語言。 (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an inefficient C program, since GHC compiles Haskell to C).) My question is, why? (Haskellers 最喜歡做的事情是嘗試達到 C 的 5% 以內(甚至擊敗它,但這意味着您使用的是低效的 C 程序,因爲 GHC 將 Haskell 編譯爲 C)。)我的問題是,爲什麼?

Haskell is declarative and based on lambda calculus. Haskell 是聲明式的,基於 lambda 演算。 Machine architectures are clearly imperative, being based on turing machines, roughly.機器架構顯然是必不可少的,大致基於圖靈機。 Indeed, Haskell doesn't even have a specific evaluation order.事實上,Haskell 甚至沒有特定的求值順序。 Also, instead of dealing with machine data types, you make algebraic data types all the time.此外,您一直在創建代數數據類型,而不是處理機器數據類型。

Weirdest of all though is higher order functions.最奇怪的是高階函數。 You would think that creating functions on the fly, and throwing them around, would make a program slower.您可能會認爲動態創建函數並將它們扔來扔去會使程序變慢。 But using higher order functions actually makes Haskell faster. 但是使用高階函數實際上使 Haskell 更快。 Indeed, it seems that, to optimize Haskell code, you need to make it more elegant and abstract instead of more machine-like.確實,似乎要優化 Haskell 代碼,您需要使其更優雅和抽象,而不是更像機器。 None of Haskell's more advanced features seem to even affect its performance, if they don't improve it.如果 Haskell 的任何更高級的功能不改進它,它們似乎都不會影響它的性能。

Sorry if this is sounding ranty, but here is my question: Why is Haskell (compiled with GHC) so fast, considering its abstract nature and differences from physical machines?對不起,如果這聽起來很囉嗦,但這是我的問題:爲什麼 Haskell(用 GHC 編譯)如此之快,考慮到它的抽象性質和與物理機器的區別?

Note: The reason I say C and other imperative languages are somewhat similar to Turing Machines (but not to the extent that Haskell is similar to Lambda Calculus) is that in an imperative language, you have a finite number of states (aka line number), along with a Tape (the ram), such that the state and the current tape determine what to do to the tape.注意:我說 C 和其他命令式語言有點類似於圖靈機的原因(但不像 Haskell 類似於 Lambda 演算那樣)是因爲在命令式語言中,你有有限數量的狀態(也就是行號) ,以及磁帶(內存),以便狀態和當前磁帶確定對磁帶執行的操作。 See the Wikipedia entry, Turing machine equivalents , for the transition from Turing Machines to computers.有關從圖靈機到計算機的過渡,請參閱 Wikipedia 條目圖靈機等效項。


解決方案:

參考一: https://stackoom.com/question/2MyMy
參考二: Why is Haskell (GHC) so darn fast?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章