The Julia Language We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as
(
Read more... )
В три раза код с for() и рекурсией становится быстрее. Естественно что вылизанный до предела код без циклов и вызовов функций компиляцией тормозится на время компиляции.
А pisum в 7 раз быстрее стал.
> library(compiler)
> enableJIT(0)
[1] 0
> assert = function(bool) {
+ if (!bool) stop('Assertion failed')
+ }
>
> timeit = function(name, f, ..., times=5) {
+ tmin = Inf
+ for (t in 1:times) {
+ t = system.time(f(...))["elapsed"]
+ if (t < tmin) tmin = t
+ }
+ cat(sprintf("r,%s,%.8f\n", name, tmin*1000))
+ }
> fib = function(n) {
+ if (n < 2) {
+ return(n)
+ } else {
+ return(fib(n-1) + fib(n-2))
+ }
+ }
>
> assert(fib(20) == 6765)
> timeit("fib", fib, 20)
r,fib,64.00000000
> timeit("fib", fib, 20)
r,fib,63.00000000
> enableJIT(3)
[1] 0
> enableJIT(3)
[1] 3
> timeit("fib", fib, 20)
r,fib,19.00000000
> timeit("fib", fib, 20)
r,fib,20.00000000
> timeit("fib", fib, 20)
r,fib,20.00000000
--------------------8<-----------------
> enableJIT(0)
[1] 0
> timeit('quicksort', sortperf, 5000)
r,quicksort,406.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,393.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,390.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,390.00000000
> enableJIT(3)
[1] 0
> enableJIT(3)
[1] 3
> timeit('quicksort', sortperf, 5000)
r,quicksort,131.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,121.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,122.00000000
> timeit('quicksort', sortperf, 5000)
r,quicksort,123.00000000
> enableJIT(0)
[1] 3
> enableJIT(0)
[1] 0
> ## pi_sum ##
>
> pisum = function() {
+ t = 0.0
+ for (j in 1:500) {
+ t = 0.0
+ for (k in 1:10000) {
+ t = t + 1.0/(k*k)
+ }
+ }
+ return(t)
+ }
>
> assert(abs(pisum()-1.644834071848065) < 1e-12);
> timeit("pi_sum", pisum, times=1)
r,pi_sum,12763.00000000
> timeit("pi_sum", pisum, times=1)
r,pi_sum,13190.00000000
> enableJIT(3)
[1] 0
> enableJIT(3)
[1] 3
> assert(abs(pisum()-1.644834071848065) < 1e-12);
> timeit("pi_sum", pisum, times=1)
r,pi_sum,1845.00000000
> timeit("pi_sum", pisum, times=1)
r,pi_sum,1850.00000000
>
<\pre>
Reply
Leave a comment