Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type instability of the std function? #23877

Closed
goszlanyi opened this issue Sep 25, 2017 · 4 comments
Closed

Type instability of the std function? #23877

goszlanyi opened this issue Sep 25, 2017 · 4 comments

Comments

@goszlanyi
Copy link

Please consider the following code:

function fun1(x)
    delta = std(x)
    
    for i in eachindex(x)
        if x[i]<delta
           x[i] = -x[i]
        end
    end

    return x
end

function fun2(x)
    delta::Float64 = std(x)
    
    for i in eachindex(x)
        if x[i]<delta
           x[i] = -x[i]
        end
    end

    return x
end


x = rand(100_000);

@time for i=1:1000 fun1(x); end
@time for i=1:1000 fun2(x); end

2.233152 seconds (100.01 M allocations: 1.490 GiB, 4.51% gc time)
0.404369 seconds (8.43 k allocations: 360.187 KiB)

This is a significant speed difference.
Does it come from from the type instability of the std function?

julia> versioninfo()
Julia Version 0.6.0
Commit 9036443* (2017-06-19 13:05 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4790S CPU @ 3.20GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Sep 25, 2017

I think this is coming from the fact that std passes a function as a keyword argument (mean=mean) to var and I don't think that we can infer keyword function arguments like we can positional function arguments. Nothing fundamental here, it's just a matter of optimization elbow grease.

@goszlanyi
Copy link
Author

goszlanyi commented Sep 26, 2017

Thank you for the clear explanation.
Is there already a more general issue for the inference of functions as keyword arguments?

@yuyichao
Copy link
Contributor

Ref #9551 #21794 #23548 #21915 and all linked issues.

Dup of #21811

@goszlanyi
Copy link
Author

goszlanyi commented Sep 26, 2017

Huh. This is a substantial history.
Thank you for linking it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants