Skip to content

Instantly share code, notes, and snippets.

@athos
Last active January 1, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athos/8081180 to your computer and use it in GitHub Desktop.
Save athos/8081180 to your computer and use it in GitHub Desktop.
「Clojureが遅いという話」http://mopemope.hatenablog.com/entry/2013/10/16/145955 についての(手元の環境での)検証結果。
nREPL server started on port 55753 on host 127.0.0.1
REPL-y 0.3.0
Clojure 1.5.1
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (dotimes [i 5] (time (reduce unchecked-add (map unchecked-add (range 1000000) (range 1000000)))))
"Elapsed time: 2554.02 msecs"
"Elapsed time: 655.573 msecs"
"Elapsed time: 3407.841 msecs"
"Elapsed time: 727.785 msecs"
"Elapsed time: 502.274 msecs"
nil
user=> (dotimes [i 5] (time (reduce unchecked-add (map unchecked-add (range 1000000) (range 1000000)))))
"Elapsed time: 627.238 msecs"
"Elapsed time: 602.808 msecs"
"Elapsed time: 465.695 msecs"
"Elapsed time: 480.631 msecs"
"Elapsed time: 503.045 msecs"
nil
user=> (dotimes [i 5] (time (reduce unchecked-add (map unchecked-add (range 1000000) (range 1000000)))))
"Elapsed time: 478.023 msecs"
"Elapsed time: 564.041 msecs"
"Elapsed time: 507.358 msecs"
"Elapsed time: 471.146 msecs"
"Elapsed time: 465.057 msecs"
nil
; 本当に実行効率が重要ならより低レベル(だが高速)な書き方をすればいいだけ。
user=> (set! *unchecked-math* true)
true
user=> (dotimes [i 5] (time (loop [x 0, y 0, n 0] (if (= x 1000000) n (recur (inc x) (inc y) (+ x y n))))))
"Elapsed time: 6.68 msecs"
"Elapsed time: 25.188 msecs"
"Elapsed time: 3.505 msecs"
"Elapsed time: 3.79 msecs"
"Elapsed time: 2.677 msecs"
nil
user=>
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> def f(x):
... return reduce(lambda x, y: x + y, (map (lambda x, y: x + y, xrange(1, x), xrange(1, x))))
...
>>> 1000*timeit.timeit("f(1000000)", "from __main__ import f", number = 1)
682.3670864105225
>>> 1000*timeit.timeit("f(1000000)", "from __main__ import f", number = 1)
602.5450229644775
>>> 1000*timeit.timeit("f(1000000)", "from __main__ import f", number = 1)
586.9967937469482
>>> 1000*timeit.timeit("f(1000000)", "from __main__ import f", number = 1)
576.725959777832
>>> 1000*timeit.timeit("f(1000000)", "from __main__ import f", number = 1)
584.7311019897461
# Pythonの方はより効率的な書き方はよく分からない
>>> def g(x):
... total = 0
... for i in xrange(x):
... total = total + 2*i
... return total
...
>>> 1000*timeit.timeit("g(1000000)", "from __main__ import g", number = 1)
167.27280616760254
>>> 1000*timeit.timeit("g(1000000)", "from __main__ import g", number = 1)
192.39306449890137
>>> 1000*timeit.timeit("g(1000000)", "from __main__ import g", number = 1)
156.57711029052734
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment