In Julia is Int64 slower than Int32?

Viewed 239

I am new to Julia. Just a quick question, in Julia, will using type Int64 slow down the calculation comparing with using Int32? Like,

i=1::Int64
j=1::Int64

and I want to calculate

i+j

If I define i and j as Int32, will that making i+j faster than if i and j are Int64? Thanks!

I know in Fortran, int 8 will be much slower than int 4. Not sure if it is the same in Julia

2 Answers

First of all, all of this is architecture dependent. I will be assuming relatively modern 64 bit x86 CPUs for this answer. For scalar calculations, Int64 and Int32 will be the same speed. (Int16/Int8 are roughly the same, though are occasionally slightly slower). For vectorized computation, i.e. rand(Int64,100) .+ rand(Int64,100), Int64 will be about 2x slower than Int32, due to better cache usage, and higher vectorization widths (for AVX-2/AVX-512 etc).

The time will depend on the size of the object.

Consider the following function that does some multiplication, addition and comparison of integers:

min200(T) = minimum(x*x+x for x in UnitRange{T}(1:200))

Here are the times using BenchmarkTools:

julia> @btime min200(Int16);
  9.409 ns (0 allocations: 0 bytes)

julia> @btime min200(Int32);
  14.329 ns (0 allocations: 0 bytes)

julia> @btime min200(Int64);
  47.267 ns (0 allocations: 0 bytes)

julia> @btime min200(Int128);
  256.160 ns (0 allocations: 0 bytes)

Note that this difference goes down to the assembly code. Let us see how an addition of two integer number gets compiled.

julia> @code_native +(Int32(5), Int32(7))
        .text
; ┌ @ int.jl:87 within `+`
        pushq   %rbp
        movq    %rsp, %rbp
        leal    (%rcx,%rdx), %eax
        popq    %rbp
        retq
        nopl    (%rax)
; └

julia> @code_native +(5,7)
        .text
; ┌ @ int.jl:87 within `+`
        pushq   %rbp
        movq    %rsp, %rbp
        leaq    (%rcx,%rdx), %rax
        popq    %rbp
        retq
        nopw    (%rax,%rax)
; └

You can see that the 32-bit addition used 32-bit registry %eax vs 64-bit %rax in the second code.

Moreover, when you using features such as @simd or GPU computing via CuArrays those differences might turn out to be even more significant.

Related