有人告诉我,并且我相信 Clojure 比 Python 更快。为什么这个 Python 代码比这个看似等效的 Clojure 代码运行得更快?Python 在编译时是否做了一些优化?
\ndef find_fifty(n,memory=1,count=0):\n if memory < 0.5:\n return count\n else:\n return find_fifty(n,memory*(1 - count/n),count+1)\n\nfind_fifty(100000)\xe2\x80\x8a\nRun Code Online (Sandbox Code Playgroud)\n(defn fifty \n ([n] (fifty n 1 0))\n ([n memory count]\n (if (< memory 0.5)\n count\n (recur n\n (* memory (- 1 (/ count n)))\n (inc count)))))\n\n(fifty 100000)\nRun Code Online (Sandbox Code Playgroud)\n感觉 Clojure 的时间复杂度比 Python 高。Python函数可以接收比Clojure函数高很多倍的输入,才在运行时上有显着的提升。
\n更新 - Clojure 修复
\n(defn fifty \n ([n] (fifty (float n) 1 0))\n ([n memory count]\n (if (< memory …Run Code Online (Sandbox Code Playgroud)