How to tell if Val{T} is using a fast route

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to tell if Val{T} is using a fast route

FANG Colin
In performance tips, it says

Essentially, Val{T}works only when T is either hard-coded (Val{3}) or already specified in the type-domain.

Suppose I have

ff(::Type{Val{1}}) = 1

I guess the following is on a slower route.

x = 1
a = ff(::Type{Val{x}})

And maybe this one can be determined in compile time

const y = 1
a = ff(::Type{Val{y}})

How can I tell if it is fast or slow? @code_warntype doesn't tell the difference here?
Reply | Threaded
Open this post in threaded view
|

Re: How to tell if Val{T} is using a fast route

Yichao Yu
On Wed, Nov 16, 2016 at 10:06 AM, FANG Colin <[hidden email]> wrote:

> In performance tips, it says
>
> Essentially, Val{T}works only when T is either hard-coded (Val{3}) or
> already specified in the type-domain.
>
> Suppose I have
>
> ff(::Type{Val{1}}) = 1
>
> I guess the following is on a slower route.
>
> x = 1
> a = ff(::Type{Val{x}})
>
> And maybe this one can be determined in compile time
>
> const y = 1
> a = ff(::Type{Val{y}})
>
> How can I tell if it is fast or slow? @code_warntype doesn't tell the
> difference here?

Both of these are slow since it's in the global scope. (although I'm
not sure exactly what you are checking since none of these are valid
syntax.) If you are doing it in a local scope then code_warntype
should be enough.
Reply | Threaded
Open this post in threaded view
|

Re: How to tell if Val{T} is using a fast route

FANG Colin
Typo, should be

module ...

ff(x::Type{Val{1}}) = 1

x = 1
a = ff(Val{x})

const y = 1
a = ff(Val{y})

end

Reply | Threaded
Open this post in threaded view
|

Re: How to tell if Val{T} is using a fast route

Simon Danisch
In reply to this post by FANG Colin
... And really should be, if you want to use code typed:

# module scope

ff(x::Type{Val{1}}) = 1

const y = 1

function test()
    # local scope
    x = 1
    a = ff(Val{x})
    c = ff(Val{y})
    d = ff(Val{1})
end

@code_warntype test()


Best,
Simon


Am Mittwoch, 16. November 2016 16:06:31 UTC+1 schrieb FANG Colin:
In performance tips, it says

Essentially, Val{T}works only when T is either hard-coded (Val{3}) or already specified in the type-domain.

Suppose I have

ff(::Type{Val{1}}) = 1

I guess the following is on a slower route.

x = 1
a = ff(::Type{Val{x}})

And maybe this one can be determined in compile time

const y = 1
a = ff(::Type{Val{y}})

How can I tell if it is fast or slow? @code_warntype doesn't tell the difference here?
Reply | Threaded
Open this post in threaded view
|

Re: How to tell if Val{T} is using a fast route

Chris Rackauckas
In reply to this post by FANG Colin
The top level scope of a module is still a global (interactive scope).

On Wednesday, November 16, 2016 at 7:59:04 AM UTC-8, FANG Colin wrote:
Typo, should be

module ...

ff(x::Type{Val{1}}) = 1

x = 1
a = ff(Val{x})

const y = 1
a = ff(Val{y})

end