Frequently Asked Questions The Julia Language
Frequently Asked Questions The Julia Language
General
The basic issue is that there is nothing special about Julia's compiler: we use a commonplace compiler
(LLVM) with no “secret sauce” that other language developers don't know about. Indeed, Julia's compiler is
in many ways much simpler than those of other dynamic languages (e.g. PyPy or LuaJIT). Julia's
performance advantage derives almost entirely from its front-end: its language semantics allow a well-
written Julia program to give more opportunities to the compiler to generate ef cient code and memory
layouts. If you tried to compile Matlab or Python code to Julia, our compiler would be limited by the
semantics of Matlab or Python to producing code no better than that of existing compilers for those
languages (and probably worse). The key role of semantics is also why several existing Python compilers
(like Numba and Pythran) only attempt to optimize a small subset of the language (e.g. operations on
Numpy arrays and scalars), and for this subset they are already doing at least as well as we could for the
same semantics. The people working on those projects are incredibly smart and have accomplished
amazing things, but retro tting a compiler onto a language that was designed to be interpreted is a very
dif cult problem.
Julia's advantage is that good performance is not limited to a small subset of “built-in” types and
operations, and one can write high-level type-generic code that works on arbitrary user-de ned types
while remaining fast and memory-ef cient. Types in languages like Python simply don't provide enough
information to the compiler for similar capabilities, so as soon as you used those languages as a Julia front-
end you would be stuck.
For similar reasons, automated translation to Julia would also typically generate unreadable, slow, non-
idiomatic code that would not be a good starting point for a native Julia port from another language.
https://docs.julialang.org/en/v1/manual/faq/ 1/23
5/6/2020 Frequently Asked Questions · The Julia Language
On the other hand, language interoperability is extremely useful: we want to exploit existing high-quality
code in other languages from Julia (and vice versa)! The best way to enable this is not a transpiler, but
rather via easy inter-language calling facilities. We have worked hard on this, from the built-in ccall
intrinsic (to call C and Fortran libraries) to JuliaInterop packages that connect Julia to Python, Matlab, C++,
and more.
If memory usage is your concern, you can always replace objects with ones that consume less memory. For
example, if A is a gigabyte-sized array that you no longer need, you can free the memory with A =
nothing . The memory will be released the next time the garbage collector runs; you can force this to
happen with gc() . Moreover, an attempt to use A will likely result in an error, because most methods are
not de ned on type Nothing .
While this can be inconvenient when you are developing new code, there's an excellent workaround.
Modules can be replaced by rede ning them, and so if you wrap all your new code inside a module you can
rede ne types and constants. You can't import the type names into Main and then expect to be able to
rede ne them there, but you can use the module name to resolve the scope. In other words, while
developing you might use a work ow something like this:
obj3 = MyModule.someotherfunction(obj2, c)
...
Scripting
#!/bin/bash
#=
exec julia --color=yes --startup-file=no "${BASH_SOURCE[0]}" "$@"
=#
In the example above, the code between #= and =# is run as a bash script. Julia ignores this part since it is a
multi-line comment for Julia. The Julia code after =# is ignored by bash since it stops parsing the le once it
reaches to the exec statement.
Note
https://docs.julialang.org/en/v1/manual/faq/ 3/23
5/6/2020 Frequently Asked Questions · The Julia Language
#!/bin/bash
#=
exec julia --color=yes --startup-file=no -e 'include(popfirst!(ARGS))' \
"${BASH_SOURCE[0]}" "$@"
=#
instead. Note that with this strategy PROGRAM_FILE will not be set.
Functions
julia> x = 10
10
julia> change_value!(x)
17
julia> x # x is unchanged!
10
In Julia, the binding of a variable x cannot be changed by passing x as an argument to a function. When
calling change_value!(x) in the above example, y is a newly created variable, bound initially to the value
of x , i.e. 10 ; then y is rebound to the constant 17 , while the variable x of the outer scope is left untouched.
But here is a thing you should pay attention to: suppose x is bound to an object of type Array (or any other
mutable type). From within the function, you cannot "unbind" x from this Array, but you can change its
content. For example:
https://docs.julialang.org/en/v1/manual/faq/ 4/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> x = [1,2,3]
3-element Array{Int64,1}:
1
2
3
julia> change_array!(x)
5
julia> x
3-element Array{Int64,1}:
5
2
3
Here we created a function change_array! , that assigns 5 to the rst element of the passed array (bound
to x at the call site, and bound to A within the function). Notice that, after the function call, x is still bound
to the same array, but the content of that array changed: the variables A and x were distinct bindings
referring to the same mutable Array object.
1. Use import :
import Foo
function bar(...)
# ... refer to Foo symbols via Foo.baz ...
end
This loads the module Foo and de nes a variable Foo that refers to the module, but does not import
any of the other symbols from the module into the current namespace. You refer to the Foo symbols
by their quali ed names Foo.bar etc.
2. Wrap your function in a module:
https://docs.julialang.org/en/v1/manual/faq/ 5/23
5/6/2020 Frequently Asked Questions · The Julia Language
module Bar
export bar
using Foo
function bar(...)
# ... refer to Foo.baz as simply baz ....
end
end
using Bar
This imports all the symbols from Foo , but only inside the module Bar .
julia> printargs(1, 2, 3)
Tuple{Int64,Int64,Int64}
Arg #1 = 1
Arg #2 = 2
Arg #3 = 3
If Julia were a language that made more liberal use of ASCII characters, the slurping operator might have
been written as <-... instead of ... .
https://docs.julialang.org/en/v1/manual/faq/ 6/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> x = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> threeargs(x...)
a = 1::Int64
b = 2::Int64
c = 3::Int64
If Julia were a language that made more liberal use of ASCII characters, the splatting operator might have
been written as ...-> instead of ... .
https://docs.julialang.org/en/v1/manual/faq/ 7/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> threeint()
3
julia> threefloat()
3.0
and similarly:
julia> threetup()
(3, 3)
julia> threearr()
2-element Array{Int64,1}:
3
3
https://docs.julialang.org/en/v1/manual/faq/ 8/23
5/6/2020 Frequently Asked Questions · The Julia Language
end
unstable (generic function with 1 method)
It returns either an Int or a Float64 depending on the value of its argument. Since Julia can't predict the
return type of this function at compile-time, any computation that uses it will have to guard against both
types possibly occurring, making generation of fast machine code dif cult.
julia> sqrt(-2.0)
ERROR: DomainError with -2.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Com
Stacktrace:
[...]
This behavior is an inconvenient consequence of the requirement for type-stability. In the case of sqrt ,
most users want sqrt(2.0) to give a real number, and would be unhappy if it produced the complex
number 1.4142135623730951 + 0.0im . One could write the sqrt function to switch to a complex-valued
output only when passed a negative number (which is what sqrt does in some other languages), but then
the result would not be type-stable and the sqrt function would have poor performance.
In these and other cases, you can get the result you want by choosing an input type that conveys your
willingness to accept an output type in which the result can be represented:
julia> sqrt(-2.0+0im)
0.0 + 1.4142135623730951im
of constraints and relationships may be expressed through additional type parameters that are computed
and enforced within the type's constructors.
As an example, consider
where the user would like to enforce that the third type parameter is always the second plus one. This can
be implemented with an explicit type parameter that is checked by an inner constructor method (where it
can be combined with other checks):
struct ConstrainedType{T,N,M}
A::Array{T,N}
B::Array{T,M}
function ConstrainedType(A::Array{T,N}, B::Array{T,M}) where {T,N,M}
N + 1 == M || throw(ArgumentError("second argument should have one more axis"
new{T,N,M}(A, B)
end
end
This check is usually costless, as the compiler can elide the check for valid concrete types. If the second
argument is also computed, it may be advantageous to provide an outer constructor method that performs
this calculation:
julia> typemax(Int)
9223372036854775807
julia> ans+1
-9223372036854775808
https://docs.julialang.org/en/v1/manual/faq/ 10/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> -ans
-9223372036854775808
julia> 2*ans
0
Clearly, this is far from the way mathematical integers behave, and you might think it less than ideal for a
high-level programming language to expose this to the user. For numerical work where ef ciency and
transparency are at a premium, however, the alternatives are worse.
One alternative to consider would be to check each integer operation for over ow and promote results to
bigger integer types such as Int128 or BigInt in the case of over ow. Unfortunately, this introduces
major overhead on every integer operation (think incrementing a loop counter) – it requires emitting code
to perform run-time over ow checks after arithmetic instructions and branches to handle potential
over ows. Worse still, this would cause every computation involving integers to be type-unstable. As we
mentioned above, type-stability is crucial for effective generation of ef cient code. If you can't count on the
results of integer operations being integers, it's impossible to generate fast, simple code the way C and
Fortran compilers do.
A variation on this approach, which avoids the appearance of type instability is to merge the Int and
BigInt types into a single hybrid integer type, that internally changes representation when a result no
longer ts into the size of a machine integer. While this super cially avoids type-instability at the level of
Julia code, it just sweeps the problem under the rug by foisting all of the same dif culties onto the C code
implementing this hybrid integer type. This approach can be made to work and can even be made quite fast
in many cases, but has several drawbacks. One problem is that the in-memory representation of integers
and arrays of integers no longer match the natural representation used by C, Fortran and other languages
with native machine integers. Thus, to interoperate with those languages, we would ultimately need to
introduce native integer types anyway. Any unbounded representation of integers cannot have a xed
number of bits, and thus cannot be stored inline in an array with xed-size slots – large integer values will
always require separate heap-allocated storage. And of course, no matter how clever a hybrid integer
implementation one uses, there are always performance traps – situations where performance degrades
unexpectedly. Complex representation, lack of interoperability with C and Fortran, the inability to
represent integer arrays without additional heap storage, and unpredictable performance characteristics
make even the cleverest hybrid integer implementations a poor choice for high-performance numerical
work.
An alternative to using hybrid integers or promoting to BigInts is to use saturating integer arithmetic,
where adding to the largest integer value leaves it unchanged and likewise for subtracting from the
smallest integer value. This is precisely what Matlab™ does:
>> int64(9223372036854775807)
https://docs.julialang.org/en/v1/manual/faq/ 11/23
5/6/2020 Frequently Asked Questions · The Julia Language
ans =
9223372036854775807
>> int64(9223372036854775807) + 1
ans =
9223372036854775807
>> int64(-9223372036854775808)
ans =
-9223372036854775808
>> int64(-9223372036854775808) - 1
ans =
-9223372036854775808
At rst blush, this seems reasonable enough since 9223372036854775807 is much closer to
9223372036854775808 than -9223372036854775808 is and integers are still represented with a xed
size in a natural way that is compatible with C and Fortran. Saturated integer arithmetic, however, is deeply
problematic. The rst and most obvious issue is that this is not the way machine integer arithmetic works,
so implementing saturated operations requires emitting instructions after each machine integer operation
to check for under ow or over ow and replace the result with typemin(Int) or typemax(Int) as
appropriate. This alone expands each integer operation from a single, fast instruction into half a dozen
instructions, probably including branches. Ouch. But it gets worse – saturating integer arithmetic isn't
associative. Consider this Matlab computation:
>> n = int64(2)^62
4611686018427387904
>> n + (n - 1)
9223372036854775807
>> (n + n) - 1
9223372036854775806
This makes it hard to write many basic integer algorithms since a lot of common techniques depend on the
fact that machine addition with over ow is associative. Consider nding the midpoint between integer
values lo and hi in Julia using the expression (lo + hi) >>> 1 :
https://docs.julialang.org/en/v1/manual/faq/ 12/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> n = 2^62
4611686018427387904
See? No problem. That's the correct midpoint between 2^62 and 2^63, despite the fact that n + 2n is
-4611686018427387904. Now try it in Matlab:
>> (n + 2*n)/2
ans =
4611686018427387904
Oops. Adding a >>> operator to Matlab wouldn't help, because saturation that occurs when adding n and
2n has already destroyed the information necessary to compute the correct midpoint.
Not only is lack of associativity unfortunate for programmers who cannot rely it for techniques like this,
but it also defeats almost anything compilers might want to do to optimize integer arithmetic. For example,
since Julia integers use normal machine integer arithmetic, LLVM is free to aggressively optimize simple
little functions like f(k) = 5k-1 . The machine code for this function is just this:
The actual body of the function is a single leaq instruction, which computes the integer multiply and add at
once. This is even more bene cial when f gets inlined into another function:
Since the call to f gets inlined, the loop body ends up being just a single leaq instruction. Next, consider
what happens if we make the number of loop iterations xed:
julia> code_native(g,(Int,))
.text
Filename: none
pushq %rbp
movq %rsp, %rbp
Source line: 3
imulq $9765625, %rdi, %rax # imm = 0x9502F9
addq $-2441406, %rax # imm = 0xFFDABF42
Source line: 5
popq %rbp
https://docs.julialang.org/en/v1/manual/faq/ 14/23
5/6/2020 Frequently Asked Questions · The Julia Language
retq
nopw %cs:(%rax,%rax)
Because the compiler knows that integer addition and multiplication are associative and that
multiplication distributes over addition – neither of which is true of saturating arithmetic – it can optimize
the entire loop down to just a multiply and an add. Saturated arithmetic completely defeats this kind of
optimization since associativity and distributivity can fail at each loop iteration, causing different outcomes
depending on which iteration the failure occurs in. The compiler can unroll the loop, but it cannot
algebraically reduce multiple operations into fewer equivalent operations.
The most reasonable alternative to having integer arithmetic silently over ow is to do checked arithmetic
everywhere, raising errors when adds, subtracts, and multiplies over ow, producing values that are not
value-correct. In this blog post, Dan Luu analyzes this and nds that rather than the trivial cost that this
approach should in theory have, it ends up having a substantial cost due to compilers (LLVM and GCC) not
gracefully optimizing around the added over ow checks. If this improves in the future, we could consider
defaulting to checked integer arithmetic in Julia, but for now, we have to live with the possibility of
over ow.
In the meantime, over ow-safe integer operations can be achieved through the use of external libraries
such as SaferIntegers.jl. Note that, as stated previously, the use of these libraries signi cantly increases the
execution time of code using the checked integer types. However, for limited usage, this is far less of an
issue than if it were used for all integer operations. You can follow the status of the discussion here.
julia> Foo.foo()
ERROR: On worker 2:
UndefVarError: Foo not defined
Stacktrace:
[...]
The closure x->x carries a reference to Foo , and since Foo is unavailable on node 2, an UndefVarError is
thrown.
https://docs.julialang.org/en/v1/manual/faq/ 15/23
5/6/2020 Frequently Asked Questions · The Julia Language
Globals under modules other than Main are not serialized by value to the remote node. Only a reference is
sent. Functions which create global bindings (except under Main ) may cause an UndefVarError to be
thrown later.
julia> Foo.foo()
ERROR: On worker 2:
UndefVarError: gvar not defined
Stacktrace:
[...]
In the above example, @everywhere module Foo de ned Foo on all nodes. However the call to Foo.foo()
created a new global binding gvar on the local node, but this was not found on node 2 resulting in an
UndefVarError error.
Note that this does not apply to globals created under module Main . Globals under module Main are
serialized and new bindings created under Main on the remote node.
julia> remotecall_fetch(()->gvar_self, 2)
"Node1"
julia> remotecall_fetch(varinfo, 2)
name size summary
––––––––– –––––––– –––––––
Base Module
Core Module
Main Module
gvar_self 13 bytes String
This does not apply to function or struct declarations. However, anonymous functions bound to global
variables are serialized as can be seen below.
julia> bar() = 1
bar (generic function with 1 method)
https://docs.julialang.org/en/v1/manual/faq/ 16/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> remotecall_fetch(bar, 2)
ERROR: On worker 2:
UndefVarError: #bar not defined
[...]
julia> remotecall_fetch(anon_bar, 2)
1
Note that you can also use string(...) to concatenate strings (and other values converted to strings);
similarly, repeat can be used instead of ^ to repeat strings. The interpolation syntax is also useful for
constructing strings.
The reason this is important enough to have been given separate syntax is that you don't want to
accidentally extend a function that you didn't know existed, because that could easily cause a bug. This is
most likely to happen with a method that takes a common type like a string or integer, because both you
and the other module could de ne a method to handle such a common type. If you use import , then you'll
replace the other module's implementation of bar(s::AbstractString) with your new implementation,
which could easily do something completely different (and break all/many future usages of the other
functions in module Foo that depend on calling bar).
https://docs.julialang.org/en/v1/manual/faq/ 17/23
5/6/2020 Frequently Asked Questions · The Julia Language
Some functions are used only for their side effects, and do not need to return a value. In these cases, the
convention is to return the value nothing , which is just a singleton object of type Nothing . This is an
ordinary type with no elds; there is nothing special about it except for this convention, and that the REPL
does not print anything for it. Some language constructs that would not otherwise have a value also yield
nothing , for example if false; end .
For situations where a value x of type T exists only sometimes, the Union{T, Nothing} type can be used
for function arguments, object elds and array element types as the equivalent of Nullable , Option or
Maybe in other languages. If the value itself can be nothing (notably, when T is Any ), the Union{Some{T},
Nothing} type is more appropriate since x == nothing then indicates the absence of a value, and x ==
Some(nothing) indicates the presence of a value equal to nothing . The something function allows
unwrapping Some objects and using a default value instead of nothing arguments. Note that the compiler
is able to generate ef cient code when working with Union{T, Nothing} arguments or elds.
To represent missing data in the statistical sense ( NA in R or NULL in SQL), use the missing object. See the
Missing Values section for more details.
The empty tuple ( () ) is another form of nothingness. But, it should not really be thought of as nothing but
rather a tuple of zero values.
The empty (or "bottom") type, written as Union{} (an empty union type), is a type with no values and no
subtypes (except itself). You will generally not need to use this type.
Memory
While this behavior might surprise some, the choice is deliberate. The main reason is the presence of
immutable objects within Julia, which cannot change their value once created. Indeed, a number is an
https://docs.julialang.org/en/v1/manual/faq/ 18/23
5/6/2020 Frequently Asked Questions · The Julia Language
immutable object; the statements x = 5; x += 1 do not modify the meaning of 5 , they modify the value
bound to x . For an immutable, the only way to change the value is to reassign it.
After a call like x = 5; y = power_by_squaring(x, 4) , you would get the expected result: x == 5 && y
== 625 . However, now suppose that *= , when used with matrices, instead mutated the left hand side.
There would be two problems:
For general square matrices, A = A*B cannot be implemented without temporary storage: A[1,1]
gets computed and stored on the left hand side before you're done using it on the right hand side.
Suppose you were willing to allocate a temporary for the computation (which would eliminate most of
the point of making *= work in-place); if you took advantage of the mutability of x , then this function
would behave differently for mutable vs. immutable inputs. In particular, for immutable x , after the
call you'd have (in general) y != x , but for mutable x you'd have y == x .
Because supporting generic programming is deemed more important than potential performance
optimizations that can be achieved by other means (e.g., using explicit loops), operators like += and *=
work by rebinding new values.
This is happening because, while the write call is synchronous, the writing of each argument yields to
other tasks while waiting for that part of the I/O to complete.
print and println "lock" the stream during a call. Consequently changing write to println in the above
example results in:
julia> l = ReentrantLock();
Arrays
https://docs.julialang.org/en/v1/manual/faq/ 20/23
5/6/2020 Frequently Asked Questions · The Julia Language
julia> A = zeros()
0-dimensional Array{Float64,0}:
0.0
In this example, A is a mutable container that contains one element, which can be set by A[] = 1.0 and
retrieved with A[] . All zero-dimensional arrays have the same size ( size(A) == () ), and length
( length(A) == 1 ). In particular, zero-dimensional arrays are not empty. If you nd this unintuitive, here
are some ideas that might help to understand Julia's de nition.
Zero-dimensional arrays are the "point" to vector's "line" and matrix's "plane". Just as a line has no area
(but still represents a set of things), a point has no length or any dimensions at all (but still represents a
thing).
We de ne prod(()) to be 1, and the total number of elements in an array is the product of the size.
The size of a zero-dimensional array is () , and therefore its length is 1 .
Zero-dimensional arrays don't natively have any dimensions into which you index – they’re just A[] .
We can apply the same "trailing one" rule for them as for all other array dimensionalities, so you can
indeed index them as A[1] , A[1,1] , etc; see Omitted and extra indices.
It is also important to understand the differences to ordinary scalars. Scalars are not mutable containers
(even though they are iterable and de ne things like length , getindex , e.g. 1[] == 1 ). In particular, if x =
0.0 is de ned as a scalar, it is an error to attempt to change its value via x[] = 1.0 . A scalar x can be
converted into a zero-dimensional array containing it via fill(x) , and conversely, a zero-dimensional
array a can be converted to the contained scalar via a[] . Another difference is that a scalar can participate
in linear algebra operations such as 2 * rand(2,2) , but the analogous operation with a zero-dimensional
array fill(2) * rand(2,2) is an error.
using BenchmarkTools
A = randn(1000, 1000)
B = randn(1000, 1000)
@btime $A \ $B
@btime $A * $B
Since operations like this are very thin wrappers over the relevant BLAS functions, the reason for the
discrepancy is very likely to be
https://docs.julialang.org/en/v1/manual/faq/ 21/23
5/6/2020 Frequently Asked Questions · The Julia Language
Julia compiles and uses its own copy of OpenBLAS, with threads currently capped at 8 (or the number of
your cores).
Modifying OpenBLAS settings or compiling Julia with a different BLAS library, eg Intel MKL, may provide
performance improvements. You can use MKL.jl, a package that makes Julia's linear algebra use Intel MKL
BLAS and LAPACK instead of OpenBLAS, or search the discussion forum for suggestions on how to set this
up manually. Note that Intel MKL cannot be bundled with Julia, as it is not open source.
Julia Releases
You may prefer the LTS (Long Term Support) version of Julia if you are looking for a very stable code base.
The current LTS version of Julia is versioned according to SemVer as v1.0.x; this branch will continue to
recieve bug xes until a new LTS branch is chosen, at which point the v1.0.x series will no longer recieved
regular bug xes and all but the most conservative users will be advised to upgrade to the new LTS version
series. As a package developer, you may prefer to develop for the LTS version, to maximize the number of
users who can use your package. As per SemVer, code written for v1.0 will continue to work for all future
LTS and Stable versions. In general, even if targetting the LTS, one can develop and run code in the latest
Stable version, to take advantage of the improved performance; so long as one avoids using new features
(such as added library functions or new methods).
You may prefer the nightly version of Julia if you want to take advantage of the latest updates to the
language, and don't mind if the version available today occasionally doesn't actually work. As the name
implies, releases to the nightly version are made roughly every night (depending on build infrastructure
stability). In general nightly released are fairly safe to use—your code will not catch on re. However, they
may be occasional regressions and or issues that will not be found until more thorough pre-release testing.
You may wish to test against the nightly version to ensure that such regressions that affect your use case
are caught before a release is made.
https://docs.julialang.org/en/v1/manual/faq/ 22/23
5/6/2020 Frequently Asked Questions · The Julia Language
Finally, you may also consider building Julia from source for yourself. This option is mainly for those
individuals who are comfortable at the command line, or interested in learning. If this describes you, you
may also be interested in reading our guidelines for contributing.
Links to each of these download types can be found on the download page at
https://julialang.org/downloads/. Note that not all versions of Julia are available for all platforms.
https://docs.julialang.org/en/v1/manual/faq/ 23/23