I have a script.jl
which looks more or less like this:
...
function main()
a::... = function1()
b::... = function2(...)
c::... = function3(...)
A::... = function4(...)
B::... = function5(...)
C::... = function6(...)
end
main()
I cannot @time main()
because functions 1-3 are input functions and therefore their execution times depend on how fast or slow the user is.
Is there a way to time only function 4-6?
I don't know, something like this:
...
function main()
a::... = function1()
b::... = function2(...)
c::... = function3(...)
@time(
A::... = function4(...)
B::... = function5(...)
C::... = function6(...)
)
end
main()
Functions in Julia can be combined by composing or piping (chaining) them together. Function composition is when you combine functions together and apply the resulting composition to arguments. You use the function composition operator ( ∘ ) to compose the functions, so (f ∘ g)(args...) is the same as f(g(args...)) .
Julia gives the functionality of multiple dispatch. It will be explained in detail in coming chapters. Multiple dispatch refers to using many combinations of argument types to define function behaviors. Julia provides efficient, specialized, and automatic generation of code for different argument types.
Method Tables Every function in Julia is a generic function. A generic function is conceptually a single function, but consists of many definitions, or methods. The methods of a generic function are stored in a method table.
The standard way of timing things in Julia, is by use of the @time macro. Do note, that the code we want to time is put in a function . This is because everything we do at the top level in the REPL is in global scope.
Note: I guess this is just an example but the syntax C::...
is not valid Julia syntax, it's better if you provide simple but functional examples.
You can prepend each expression that you are interested in timing with a @time
macro annotation if you want independent timings:
function main()
a = function1()
b = function2(...)
c = function3(...)
@time A = function4(...)
@time B = function5(...)
@time C = function6(...)
end
main()
or:
function main()
a = function1()
b = function2(...)
c = function3(...)
@time begin
A = function4(...)
B = function5(...)
C = function6(...)
end
end
main()
This is similar to @Gomiero answer, just that in order to time several functions with the @time
macro you need to introduce a new block and stuff everything in there.
Also checkout the still unregistered packageBenchmarks
, ie:
julia> Pkg.add("https://github.com/johnmyleswhite/Benchmarks.jl.git")
julia> using Benchmarks
julia> function test()
x = 0
for i in 1:1000_000_000
x += 1
end
return x
end
test (generic function with 1 method)
julia> @time test() # JIT warmup!
0.003669 seconds (1.71 k allocations: 90.799 KB)
1000000000
julia> @time test()
0.000002 seconds (5 allocations: 176 bytes)
1000000000
julia> @benchmark test()
================ Benchmark Results ========================
Time per evaluation: 6.03 ns [5.92 ns, 6.13 ns]
Proportion of time in GC: 0.00% [0.00%, 0.00%]
Memory allocated: 0.00 bytes
Number of allocations: 0 allocations
Number of samples: 6301
Number of evaluations: 811601
R² of OLS model: 0.951
Time spent benchmarking: 2.96 s
If you want to time several expressions use the begin
block approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With