@timed to plot the time and memory consumption

Hello,
I am new to Julia. I have implemented different type of functions ( Fib 1 , Fib2, … etc ) to calculate Fibonacci numbers in different ways. How can I Use the macro @timed to plot the time and memory consumption of the various functions?

Welcome to the Julia Discourse forum!

See the docstring of the macro (?@timed), which has an example.

Also, please quote your code, and provide an MWE if you need further help.

1 Like

Hello @A.U.M and welcome.
Tamas is saying that Julia has a built in help system.

There are three modes in the Julia REPL.
the normal julia> mode where you can type in and execute code
The pkg> mode where you can add and view package information

Press the ? character and you go into the help?> mode
You can then type @timed to get some help in the timed macro. Or you can get help on functions.
Give it a try!

Hey Amrusama! Welcome!

You can find this thread useful for getting the time How to save @time output in a file or variable? I’ll try to find something for the memory consumption. Maybe some hack over @time??

UPDATE: I guess that the second value of @timed is for the time and the third is for the memory. Since the first is for the result, you can collect all three in a row. NOT working example, this is only to explain myself,

julia> for i in 1:100
           rows[i] = (@timed i^2)[1:3]
       end

julia> plot(1:100, [rows[i][1] for i in 1:100]) # for the result of ^2

julia> plot(1:100, [rows[i][2] for i in 1:100]) # for the timing

julia> plot(1:100, [rows[i][3] for i in 1:100]) # for the memory

I guess that there are better options to do this.

Note that if you want to do any serious measuring you should consider using BenchmarkTools.jl, or at very least call every method you want to test a single time before measuring the time, to avoid counting the compilation time.

2 Likes

For example, these two functions
fib1(n::Integer)::BigInt = round(BigInt, ((1+sqrt(5))/2)^n / sqrt(5))

function fib2(n::Integer)::BigInt
if n <= 1
n
else
fib2(n-1) + fib2(n-2)
end
end

how can i plot the time and memory consumption of these two functions using micro @timed ?

For example, these two functions

fib1(n::Integer)::BigInt = round(BigInt, ((1+sqrt(5))/2)^n / sqrt(5))

function fib2(n::Integer)::BigInt
if n <= 1
n
else
fib2(n-1) + fib2(n-2)
end
end

how can i plot the time and memory consumption of these two functions using micro @timed ?