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

82 views
Skip to first unread message

FANG Colin

unread,
Nov 16, 2016, 10:06:31 AM11/16/16
to julia-users
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?

Yichao Yu

unread,
Nov 16, 2016, 10:32:00 AM11/16/16
to Julia Users
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.

FANG Colin

unread,
Nov 16, 2016, 10:59:04 AM11/16/16
to julia-users
Typo, should be

module ...

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

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

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

end

Simon Danisch

unread,
Nov 16, 2016, 1:08:13 PM11/16/16
to julia-users
... 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

Chris Rackauckas

unread,
Nov 16, 2016, 2:54:37 PM11/16/16
to julia-users
The top level scope of a module is still a global (interactive scope).
Reply all
Reply to author
Forward
0 new messages