There is a speed difference, which you can see beautifully like this:

function _sum{T}(::Type{T}, A)
    r = zero(T)
    @inbounds for i in eachindex(A)
        r += T(A[i])
    end
    r
end

@code_llvm _sum(Int, rand(Int64, 10^6)) -> uses <4,Int64> vectors
@code_llvm _sum(Int, rand(Int32, 10^6)) -> uses <8,Int32> vectors
# this suggests that we can get a speed up, but if we get it still depends 
on the CPU we use.
using BenchmarkTools # so lets benchmark this
x = rand(Int32(1):Int32(10), 10^6)
@benchmark _sum($Int64, $x) # ~ 80μs
@benchmark _sum($Int32, $x) # ~ 50μs <- its faster!


Am Montag, 17. Oktober 2016 15:19:51 UTC+2 schrieb Ángel de Vicente:
>
> Hi, 
>
> probably a very basic question, but I'm just starting to play around 
> with Julia types. 
>
> I was hoping to improve the performance of a little program I wrote, so 
> I decided to try Int32 integers instead of the default Int64, but if I 
> try to use sum, it seems that it is expecting Int64 and the result is 
> Int64, defeating the purpose of working with Int32 and actually making 
> the code much slower than the naive version. 
>
> ,---- 
> | julia> typeof(sum([i for i in Int32(1):Int32(100)]))                     
>                                                                             
>                                                            | 
> | Int64 
> `---- 
>
> Do I have to write down my own Int32::sum function? I assume I'm missing 
> something quite obvious? 
>
> Thanks, 
> -- 
> Ángel de Vicente 
> http://www.iac.es/galeria/angelv/           
>

Reply via email to