Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure time of julia program?

Tags:

time

julia

If I want to calculate for things in Julia

invQa = ChebyExp(g->1/Q(g),0,1,5) 
a1Inf = ChebyExp(g->Q(g),1,10,5)
invQb = ChebyExp(g->1/Qd(g),0,1,5)
Qb1Inf = ChebyExp(g->Qd(g),1,10,5)

How can I count the time? How many seconds do i have to wait for the four things up be done? Do I put tic() at the beginning and toc() at the end?

I tried @elapsed, but no results.

like image 551
Lucas G Leite F Pollito Avatar asked Oct 10 '17 03:10

Lucas G Leite F Pollito


2 Answers

The basic way is to use

@time begin
  #code
end

But note that you never should benchmark in the global scope.

A package that can help you benchmark your code is BenchmarkTools.jl which you should check out as well.

like image 51
Chris Rackauckas Avatar answered Oct 05 '22 14:10

Chris Rackauckas


You could do something like this (I guess that g is input parameter):

function cheby_test(g::Your_Type)
    invQa = ChebyExp(g->1/Q(g),0,1,5) 
    a1Inf = ChebyExp(g->Q(g),1,10,5)
    invQb = ChebyExp(g->1/Qd(g),0,1,5)
    Qb1Inf = ChebyExp(g->Qd(g),1,10,5)
end

function test()
    g::Your_Type = small_quick  #  
    cheby_test(g)    #= function is compiled here and 
                        you like to exclude compile time from test =#
    g = real_data()
    @time cheby_test(g)  # here you measure time for real data
end

test()

I propose to call @time not in global scope if you like to get proper allocation info from time macro.

like image 42
Liso Avatar answered Oct 05 '22 15:10

Liso