When you benchmark stuff like this it is good to make a benchmark with as 
little noise in it as possible. Here is a version which removes slicing, 
bounds checking, allocations etc:

function timeit()
    A = rand(1024, 1024)
    B = similar(A)
    a = @elapsed for i = 1:size(A, 1), j = 1:size(A, 2)
        @inbounds B[i,j] = sqrt(abs2(A[i,j]))
    end

    b = @elapsed for i = 1:size(A, 1), j = 1:size(A, 2)
        @inbounds B[i,j] = abs(A[i,j])
    end

    return a,b
end

as = Float64[]
bs = Float64[]

for i in 1:20
    a, b = timeit()
    push!(as, a)
    push!(bs, b)
    println(i)
end

This gives me the results:

julia> mean(as)
0.04173319045

julia> mean(bs)
0.0417097596

which seems pretty much equal to me

On Monday, May 18, 2015 at 4:51:48 PM UTC+2, antony schutz wrote:
>
> Hello, 
>
> I just discover abs2() function :v 
>
> And I have to say that i'm really surprised that: 
> sqrt(abs2( x ))  is quicker than  abs( x ) 
>
> For a Matrix of size 512*512 and then a vector of size 512 
> 10000 realizations, the mean results are (on a macbook): 
>
> abs(x[:,:])
>
> 0.0073665214096
>
> sqrt(abs2(x[:,:]))
>
> 0.0046861180318999995
>
> abs(x[:])
>
> 1.2221291200000001e-5
>
> sqrt(abs2(x[:]))
>
> 1.15177585e-5
>
>
> Regards
>
>
>
>

Reply via email to