Julia compiles functions just-in-time (JIT)


In [12]:
function pi_sum(n)
    sum = 0
    for k = 1:n
        sum += 1.0/(k*k)
    end
    return sum
end


Out[12]:
pi_sum (generic function with 1 method)

In [15]:
# The first time you run it for a given argument type, the function is compiled
 pi_sum(1000000)


elapsed time: 0.007835417 seconds (96 bytes allocated)
Out[15]:
1.64493306684877

In [9]:
# show native machine code...
# (about the same thing you'd get if you compiled C code)
code_native(pi_sum, (Int32,))


	.text
Filename: In[5]
Source line: 4
	push	RBP
	mov	RBP, RSP
	xorps	XMM0, XMM0
	test	EDI, EDI
	jle	63
	movsxd	RAX, EDI
	mov	ECX, 1
	xorps	XMM0, XMM0
	movabs	RDX, 140275987957600
	movsd	XMM1, QWORD PTR [RDX]
Source line: 4
	mov	RDX, RCX
	imul	RDX, RDX
	xorps	XMM3, XMM3
	cvtsi2sd	XMM3, RDX
	movaps	XMM2, XMM1
	divsd	XMM2, XMM3
Source line: 3
	inc	RCX
Source line: 4
	dec	RAX
	addsd	XMM0, XMM2
	jne	-38
Source line: 6
	pop	RBP
	ret

built on LLVM

  • LLVM is a library for writing compilers
  • The Clang C compiler is also built on LLVM

Data flow inference