How to evaluate the execution time of a function?

1,881 views
Skip to first unread message

Pedro Rafael

unread,
Mar 5, 2013, 7:45:35 AM3/5/13
to julia...@googlegroups.com
I am wanting to compare the execution time of a function in R on Julia. In R, there is a function called sys.time I can pass a function as an argument for it. How to do this in Julia?

Function in R:

f = function(n=1e6){res=0; for (i in 1:n) res=res+i^2; res}
system.time(f())
## no meu computadorzinho idoso: 1.3s
library(compiler)
enableJIT(3)
system.time(f())

Function in Julia:

t0 = time();
function f()
    n = 1e6;
    res = 0;
        for i = 1:n
            res = res + i^2;
                res
        end
end
f()
println("Tempo = $(time()-t0)")

John Myles White

unread,
Mar 5, 2013, 7:54:02 AM3/5/13
to julia...@googlegroups.com
@elapsed f() is probably the easiest way to do this.

-- John

Pedro Rafael

unread,
Mar 5, 2013, 9:42:27 AM3/5/13
to julia...@googlegroups.com
Because the runtime of this code written in compiling Julia took more than running in módigo repl?

[   ],
Pedro Rafael Diniz Marinho.


2013/3/5 John Myles White <johnmyl...@gmail.com>

Jacob Quinn

unread,
Mar 5, 2013, 10:27:19 AM3/5/13
to julia...@googlegroups.com
I didn't quite catch your last statement, but one thing to note is due to the nature of the JIT compilation in Julia, getting a consistent elapsed time should exclude the first run (when the function is compiled, then run).

-Jacob

Pedro Rafael

unread,
Mar 5, 2013, 10:45:30 AM3/5/13
to julia...@googlegroups.com
But the compiled code was not meant to be more efficient? The efficiency of the compilation would only be noticed if the code would require more processor?

[   ],
Pedro Rafael Diniz Marinho.


2013/3/5 Jacob Quinn <karb...@gmail.com>

Kevin Squire

unread,
Mar 5, 2013, 12:44:06 PM3/5/13
to julia...@googlegroups.com
The first time a piece of code is seen, it is compiled internally, adding to the compile time.  After that, it will be fast:

julia> @elapsed sort(rand(10000))
0.24610693


julia
> @elapsed sort(rand(10000))
0.000843886


julia
> @elapsed sort(rand(10000))
0.003202571

Kevin Squire

unread,
Mar 5, 2013, 12:44:51 PM3/5/13
to julia...@googlegroups.com
Sorry, I meant "adding to the execution time", not "adding to the compile time".  I need my coffee... 

Cheers!
   Kevin

Pedro Rafael

unread,
Mar 5, 2013, 1:56:48 PM3/5/13
to julia...@googlegroups.com
Is there any way to get the machine code to prevent the code from being interpreted the first time it is run?

[   ],
Pedro Rafael Diniz Marinho.


2013/3/5 Kevin Squire <kevin....@gmail.com>

Kevin Squire

unread,
Mar 5, 2013, 3:22:38 PM3/5/13
to julia...@googlegroups.com

You've been asking about this a lot recently! :-)

Short answer: no.
Longer answer: as mentioned previously, it's planned, but not yet implemented.

One exception: you can modify the julia system image (or build a different one), but there are no instructions for doing this.

Kevin

Pedro Rafael

unread,
Mar 5, 2013, 8:11:55 PM3/5/13
to julia...@googlegroups.com
Então ainda não há portabilidade para o código escrito em Julia. Não é verdade? Posso dizer que hoje em dia código Julia não é portátil?

John Myles White

unread,
Mar 5, 2013, 8:14:06 PM3/5/13
to julia...@googlegroups.com
While I don't understand Portuguese well enough to catch every word in this question, I think the answer is probably: under the definition of portability you're looking for, Julia is not yet portable. Getting there will take more time and work.

 -- John

Pedro Rafael

unread,
Mar 5, 2013, 8:15:47 PM3/5/13
to julia...@googlegroups.com
Sorry, written in Portuguese.

John Myles White

unread,
Mar 5, 2013, 8:27:08 PM3/5/13
to julia...@googlegroups.com
It's ok with me. It's an interesting challenge trying to make sense of Portuguese e-mails.

 -- John

Alessandro "Jake" Andrioni

unread,
Mar 5, 2013, 8:46:00 PM3/5/13
to julia...@googlegroups.com
Se você considera portabilidade como compilar um binário que vá rodar
em qualquer máquina, ainda não, mas está nos planos. O modelo atual é
muito mais parecido com Python (você precisa de um interpretador
instalado em cada máquina, mas não precisa alterar nada do código em
si).

(sorry for the Portuguese post!)

Also, one question: are we going to get a .pyc-like feature to
store/cache compiled functions before the planned static compilation?
Reply all
Reply to author
Forward
0 new messages