While it is certainly expected that a custom type will be more performant, 
you cannot really trust your timings. You should not time with variables 
defined in global scope, and you should add a warm-up phase to compile your 
functions.

Defining all of this:


# Method 1: Type

type parameters
    p1::Float64
    p2::Float64
    p3::Float64
end

function f1(n,p)
    for i = 1:n
        p.p1 += 0.1
        p.p2 += 0.1
        p.p3 += 0.1
    end
end

# Method 2: Dictionary

function f2(n,p)
    for i = 1:n
        p["p1"] += 0.1
        p["p2"] += 0.1
        p["p3"] += 0.1
    end
end

# Time function
function correcttime()
p_method_1 = parameters(3.6, 0.6, 600.0)
p_method_2 = {"p1" => 3.6, "p2" => 0.6, "p3" => 600.0}
f1(10,p_method_1)
f2(10,p_method_2)

println("Method 1")
println(p_method_1)
@time f1(100000,p_method_1)
println(p_method_1)

println("Method 2")
println(p_method_2)
@time f2(100000,p_method_2)
println(p_method_2)
end

and then running

correcttime()

gives

Method 1

parameters(4.599999999999998,1.6000000000000003,601.0000000000002)

elapsed time: 0.000275517 seconds (0 bytes allocated)

parameters(10004.600000018865,10001.600000018854,10601.000000020967)

Method 2

{"p3"=>601.0000000000002,"p2"=>1.6000000000000003,"p1"=>4.599999999999998}

elapsed time: 0.079420021 seconds (9600000 bytes allocated)

{"p3"=>10601.000000020967,"p2"=>10001.600000018854,"p1"=>10004.600000018865}




Op dinsdag 30 september 2014 11:09:38 UTC+2 schreef [email protected]:
>
> Hi again,
>
> Thanks for all the suggestions.
> I'm a bit ashamed I overlooked the part of the documentation about types.
> I skimmed through the DataFrames documentation and I don't think there's a 
> way to access data via keys.
> As far as the two other solutions are concerned, I compared their 
> performance on a very simple test case, here it is if it can be of any 
> interest :
>
> n = 100000
>
> # Method 1: Type
>
> type parameters
>     p1::Float64
>     p2::Float64
>     p3::Float64
> end
>
> p_method_1 = parameters(3.6, 0.6, 600.0)
>
> function f1(p)
>     for i = 1:n
>         p.p1 += 0.1
>         p.p2 += 0.1
>         p.p3 += 0.1
>     end
> end
>
> # Method 2: Dictionnary
>
> p_method_2 = {"p1" => 3.6, "p2" => 0.6, "p3" => 600.0}
>
> function f2(p)
>     for i = 1:n
>         p["p1"] += 0.1
>         p["p2"] += 0.1
>         p["p3"] += 0.1
>     end
> end
>
> println("Method 1")
> println(p_method_1)
> @time f1(p_method_1)
> println(p_method_1)
>
> println("Method 2")
> println(p_method_2)
> @time f2(p_method_2)
> println(p_method_2)
>
> Here are the results:
>
> Method 1
> parameters(3.6,0.6,600.0)
> elapsed time: 0.020289272 seconds (6610188 bytes allocated)
> parameters(10003.600000018861,10000.60000001885,10600.000000020964)
> Method 2
> {"p3"=>600.0,"p2"=>0.6,"p1"=>3.6}
> elapsed time: 0.120914516 seconds (16193368 bytes allocated)
> {"p3"=>10600.000000020964,"p2"=>10000.60000001885,"p1"=>10003.600000018861}
>
> The first method is by far the most efficient.
>

Reply via email to