Hey there Freddy.  The first thing you can do to speed up your code is to
throw it inside of a function.  Simply replacing your first line (which is
"begin") with "function domytest()" speeds up your code significantly.  I
get a runtime of about 1.5 seconds from running the function versus ~70
seconds from running the original code.

I believe the reason behind this is because outside of a function, all of
these variables are treated as global variables, which cannot have the same
assumptions of type-stability that local variables can have, which slows
down computation significantly.  See this
page<http://julia.readthedocs.org/en/latest/manual/performance-tips/>for
more info, and other performance tips.
-E


On Sat, Apr 26, 2014 at 11:03 PM, Freddy Chua <[email protected]> wrote:

> This code takes 60+ secs to execute on my machine. The Java equivalent
> takes only 0.2 secs!!! Please tell me how to optimise the following
> code.begin
>
> begin
>   N = 10000
>   K = 100
>   rate = 1e-2
>   ITERATIONS = 1
>
>
>   # generate y
>
>   y = rand(N)
>
>   # generate x
>   x = rand(K, N)
>
>   # generate w
>   w = zeros(Float64, K)
>
>   tic()
>   for i=1:ITERATIONS
>     for n=1:N
>       y_hat = 0.0
>
>       for k=1:K
>         y_hat += w[k] * x[k,n]
>       end
>
>       for k=1:K
>         w[k] += rate * (y[n] - y_hat) * x[k,n]
>       end
>     end
>   end
>   toc()
> end
>
> Sorry for repeated posting, I did so to properly indent the code..
>

Reply via email to