OK, I see. Thank you very much!
On Saturday, September 24, 2016 at 11:11:41 AM UTC-5, Spencer Russell wrote: > > Take a look at the performance tips in the manual: > http://docs.julialang.org/en/release-0.5/manual/performance-tips/ > > One of the main things is to avoid accessing global variables, and > everything declared at the top-level in the REPL is global, so you > generally want to put any performance-sensitive code (and variables > accessed in that code) inside a function. > > -s > > On Sep 24, 2016, at 12:04 PM, Weicheng Zhu <min...@gmail.com <javascript:>> > wrote: > > Hi there, > Could anybody tell me what's wrong with my for loop code below and how to > improve it? > I was surprised to see it performs worse than the vectorized operation. > Thank you! > > ## Calculate the maximum number in vector x that is less or equal to 0.5 >> ## Method 1 >> x=rand(10000) >> @time maximum(x[x.<=0.5]) >> # 0.000205 seconds (23 allocations: 45.141 KB) >> ## Method 2 >> u= x[1] >> @time @inbounds for i in 2:length(x) >> if u < x[i] && x[i] <= 0.5 >> u = x[i] >> end >> end >> # 0.001233 seconds (42.40 k allocations: 818.766 KB) > > > > Best, > Weicheng > > > >