The previous timings are too fast because they are not returning bar which
makes the compiler remove a bunch of stuff.
On Wednesday, June 1, 2016 at 2:14:48 PM UTC+2, Scott Jones wrote:
>
> Is that last timing correct? That's 10x faster than I get.
>
> My version of the Julia code (I parameterized it, so that it doesn't have
> to be Float64, could be complex, integer, rational, decimal float, whatever
> floats your boat!)
>
> function foo{T<:Number}(array1::Matrix{T}, array2::Matrix{T}, array3::
> Array{T,4})
> bar = zero(T)
> @inbounds for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
> bar = bar +
> (array1[i, l] - array3[1, i, j, k])^2 +
> (array2[i, l] - array3[2, i, j, k])^2
> end
> bar
> end
>
> function testfoo()
> arr1, arr2, arr3 = rand(70, 1000), rand(70, 1000), rand(2, 70, 20, 20)
> @time foo(arr1, arr2, arr3)
> end
>
>
> and the timings:
>
> 08:04 $ gfortran /j/test.f&&./a.out
>> 0.135017 seconds
>> *julia> *
>> *testfoo() ;* 0.042858 seconds
>> *julia> *
>> *testfoo() ;* 0.047505 seconds
>> *julia> *
>> *testfoo() ;* 0.046309 seconds
>> *julia> *
>> *testfoo() ;* 0.045292 seconds
>
>
> So, Julia about 3x faster with 1/3 code, seems pretty impressive to me
> (esp. given the reputation of Fortran for numeric speed [maybe no longer
> all that true, compared to Julia, C, C++?])
>
>
> On Wednesday, June 1, 2016 at 7:51:45 AM UTC-4, Lutfullah Tomak wrote:
>>
>> Ok, I revise what I said about @ simd, according to documentation, it
>> only works for
>> one dimensional ranges and it does not allow this type for-loop and
>> throws error.
>>
>> Here is timings for me
>>
>> julia> function foo()
>> array1 = rand(70, 1000)
>> array2 = rand(70, 1000)
>> array3 = rand(2, 70, 20, 20)
>> bar = 0
>> @time for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>> bar = bar +
>> (array1[i, l] - array3[1, i, j, k])^2 +
>> (array2[i, l] - array3[2, i, j, k])^2
>> end
>> end
>> foo (generic function with 1 method)
>>
>>
>> julia> foo()
>> 1.215454 seconds (84.00 M allocations: 1.252 GB, 4.16% gc time)
>>
>>
>> julia> foo()
>> 1.308979 seconds (84.00 M allocations: 1.252 GB, 3.92% gc time)
>>
>>
>> julia> function foo()
>> array1 = rand(70, 1000)
>> array2 = rand(70, 1000)
>> array3 = rand(2, 70, 20, 20)
>> bar = 0.0
>> @time for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>> bar = bar +
>> (array1[i, l] - array3[1, i, j, k])^2 +
>> (array2[i, l] - array3[2, i, j, k])^2
>> end
>> end
>> foo (generic function with 1 method)
>>
>>
>> julia> foo()
>> 0.114811 seconds
>>
>>
>> julia> foo()
>> 0.150542 seconds
>>
>>
>> julia> function foo() array1 = rand(70, 1000)
>> array2 = rand(70, 1000)
>> array3 = rand(2, 70, 20, 20)
>> bar = 0.0
>> @time @inbounds for l = 1:1000, k = 1:20, j = 1:20, i = 1:70
>> bar = bar +
>> (array1[i, l] - array3[1, i, j, k])^2 +
>> (array2[i, l] - array3[2, i, j, k])^2
>> end
>>
>> end
>>
>> foo (generic function with 1 method)
>>
>>
>> julia> foo()
>> 0.004927 seconds
>>
>>
>>
>>