4 ms·
You don't even need to annotate things with types! > square(x) = x * x > @code_typed square(2.0) CodeInfo( 1 1 ─ %1 = Base.mul_float(%%x, %%x)
by stabbles 8y ago
You don't even need to annotate things with types!
> square(x) = x * x
> @code_typed square(2.0)
CodeInfo(
1 1 ─ %1 = Base.mul_float(%%x, %%x)::Float64
└── return %1
) => Float64
> @code_typed square(2)
CodeInfo(
1 1 ─ %1 = Base.mul_int(%%x, %%x)::Int64
└── return %1
) => Int64
Functions specialize automatically to the arguments you pass in.
- ChrisFoster 8y agoOr, to see what's really going on at a low level you have immediate access to the native assembly from the REPL: julia> square(x) = x^2 square (generic function with 1 method) julia> @code_native square(1) .text Filename: REPL[3] pushq %rbp movq %rsp, %rbp Source line: 1 imulq %rdi, %rdi movq %rdi, %rax popq %rbp retq julia> @code_native square(1.0) .text Filename: REPL[3] pushq %rbp movq %rsp, %rbp Source line: 1 mulsd %xmm0, %xmm0 popq %rbp retq
- DNF2 8y agoYou don't need to annotate function definitions with types, but you should actually annotate your composite type definitions with concrete (parametric) types for each field.