f(x) = x^3 - 2
@time f(0.3);
@time f(0.4);
@code_lowered(f(7.0)) # show f(x) in Julia's abstract syntax tree
@code_llvm f(7.0) # show f(x) in LLVM (compiler) intermediate language
@code_native f(7.0) # show f(x) in Intel IA-64 assembly language
@code_llvm(f(7.0))
@code_llvm(f(7))
sqrt(-1)
# define logistic map function
f(x) = 4*x*(1-x)
# define function that, given an f, returns iterated function f^N
function iterator(f, N)
# construct f^N
function fN(x)
for i ∈ 1:N
x = f(x)
end
x
end
fN # return f^N
end
# use iterator function to constuct millionth iterate of logistic map
fᴺ = iterator(f, 1000000)
@time x = fᴺ(0.34);
@time x = fᴺ(0.67);
@show x;
note: starting semicolon tells Julia to execute Unix shell code
; pwd
; cat fmillion.cpp
; g++ -O3 -o fmillion fmillion.cpp
; fmillion 0.67
print("t=")
@time x = fᴺ(0.67);
@show x;
fᴺ(0.0)
Speeds are comparable. Sometimes Julia is faster, sometimes C.