lindahua / Devectorize.jl

A Julia framework for delayed expression evaluation
MIT License
115 stars 17 forks source link

@devec slower than vanilla julia for 1./(1+exp(-vector)) #48

Closed pkofod closed 9 years ago

pkofod commented 9 years ago

I am calculating

phat = 1./(1+exp(-vector))

where vector is a vector of some dimension (say 8000). Ex:

using Devectorize
vector = rand(8000)
phat = zeros(8000)

@time 1

time_vanilla = [@elapsed phat = 1./(1+exp(-vector)) for i = 1:100]
time_devec = [@elapsed @devec phat = 1./(1+exp(-vector)) for i = 1:100]

alloc_vanilla = @allocated phat = 1./(1+exp(-vector))
alloc_devec = @allocated @devec phat = 1./(1+exp(-vector))

mean(time_vanilla)
mean(time_devec)
alloc_vanilla
alloc_devec

Gives me that devec is an order of magnitude slower, and allocates five times as much memory. On v0.4.

Am I using @devec incorrectly? That is, for the wrong purpose, doing it in a stupid way etc.

Edit: A straight loop gives the same time as vanilla Julia, but obviously way less memory allocation. I would have expected @devec to do exactly this.

blakejohnson commented 9 years ago

Before I get to your issue, please wrap julia macro mentions in backticks in order to not ping GitHub users, i.e. @devec, instead of directly writing it.

As far as your issue goes, it looks like you have tried to pre-allocate the result vector phat, but the way you have used it re-binds the output of your computation to a new vector, as opposed to updating the values in phat.

Re-writing things inside functions to avoid referencing variables at global scope gives something like:

function test1()
    vector = rand(8000)
    phat = zeros(8000)
    @time for i = 1:1000
        phat[:] = 1./(1+exp(-vector))
    end
end

function test2()
    vector = rand(8000)
    phat = zeros(8000)
    @time for i = 1:1000
        @devec phat[:] = 1./(1+exp(-vector))
    end
end

Note the assignment to phat[:] which updates the contents of phat rather than re-binding it. Written like this, you should see that test2 is faster than test1.

pkofod commented 9 years ago

Oh, I thought phat = updated it if it existed already! I have a lot of my code to edit then. I know it is not your job to be my help(), but is this mentioned in the documentation? Searching for [:] is kinda difficult.

Thanks!

blakejohnson commented 9 years ago

I was sure that this must be mentioned in the docs, but after searching for a few minutes, I cannot find a direct reference.

blakejohnson commented 9 years ago

I would note that the README in this repo does mention this issue in the section on "Simple References".