On Wed, Jan 22, 2014 at 3:41 PM, Patrick Foley <[email protected]
> wrote:
> Thanks!
>
> I've sorted it out and have solved my original problems.
>
> Would I get any speedup by defining
>
> function foo{TA<:Real, TB<:Real}(a::TA, b::TB)
>
> rather than
>
> function foo(a::Real, b::Real)
>
> ?
>
> My guess is .. yes? Since if I'm defining it the first way, I can compile
> versions of foo like foo(a::Int8, b::Int8) automatically, which would be
> much faster than defaulting to a foo(a::Real, b::Real) and reserving space
> for a possible Float64 each time?
>
It would generally be faster, but for the first half of the reason you
state. When you use "a::Real", the actual type has to be reasoned about at
run time, to determine which function versions to dispatch to, etc., and
that is what slows things down.
If you create a parametrized function, a specialized function is created
for each new parameter type combination, and so the type information is
baked in, so-to-speak, when the function is first called (and therefore,
compiled) with the given parameter types.
http://blog.leahhanson.us/julia-introspects.html has a nice description of
some tools that you can use to see what happens in Julia when you define
things in different ways.
Cheers!
Kevin
>
>
> On Tuesday, January 21, 2014 7:17:36 PM UTC-5, Patrick Foley wrote:
>>
>> Is there a way to get around this? I have a lot of types (foo1, foo2,
>> ....) all of which are subtypes of an abstract (bar). I want to be able to
>> define the behavior for arrays of any of the foos just by defining the
>> behavior of an array of 'bar's. Any advice?
>>
>