小编Der*_*ler的帖子

为什么此 Python 代码比其等效的 Clojure 代码更快

有人告诉我,并且我相信 Clojure 比 Python 更快。为什么这个 Python 代码比这个看似等效的 Clojure 代码运行得更快?Python 在编译时是否做了一些优化?

\n
def 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\n
Run 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)\n
Run 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)

python optimization runtime clojure

6
推荐指数
1
解决办法
346
查看次数

标签 统计

clojure ×1

optimization ×1

python ×1

runtime ×1