Looks like it's the difference between log(x::Float64) and log(x::Real). I had thought that an integer would be quickly converted to real.
On Wednesday, February 18, 2015 at 4:11:41 PM UTC-8, Peter Simon wrote: > > Not sure why this is, but when I replace 2 with 2.0 in the first line of > the function it seems to speed up by a factor of 8. > > On Wednesday, February 18, 2015 at 3:48:15 PM UTC-8, Rohan Fernando wrote: >> >> Wrapped in function, but timing did not change. >> >> function f1() >> a=2 >> for i=1:1000000 >> a+=log(a%2+1); >> end >> a >> end >> @time f1() >> >> output: >> >> elapsed time: 0.151813773 seconds (64080600 bytes allocated, 15.55% gc time) >> >> >> >> On Wednesday, February 18, 2015 at 5:19:21 PM UTC-6, Stefan Karpinski >> wrote: >>> >>> Try wrapping the Julia code in a function and try again. The slow part >>> is incrementing `a` not the exp. >>> >>> >>> http://julia.readthedocs.org/en/latest/manual/performance-tips/#avoid-global-variables >>> >>> On Wed, Feb 18, 2015 at 6:15 PM, Rohan Fernando <[email protected]> >>> wrote: >>> >>>> Here is the C++ code: >>>> >>>> >>>> #include <iostream> >>>> >>>> #include <cmath> >>>> >>>> >>>> >>>> int main(int argc, const char * argv[]) { >>>> >>>> >>>> srand (time(NULL)); >>>> >>>> >>>> clock_t t; >>>> >>>> int a=2; >>>> >>>> t = clock(); >>>> >>>> for (unsigned i=0; i<1000000; i++) { >>>> >>>> //a+=(log(a)); >>>> >>>> a+=exp(a%2); >>>> >>>> //a+=(rand()%2+1); >>>> >>>> } >>>> >>>> >>>> >>>> t = clock() - t; >>>> >>>> printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/ >>>> CLOCKS_PER_SEC); >>>> >>>> std::cout<<a<<std::endl; >>>> >>>> return 0; >>>> >>>> } >>>> >>>> >>>> The output: >>>> >>>> >>>> *It took me 22177 clicks (0.022177 seconds).* >>>> >>>> *2000001* >>>> >>>> Program ended with exit code: 0 >>>> >>>> Julia Code: >>>> >>>> a=2 >>>> @time for i=1:1000000 >>>> a+=exp(a%2); >>>> end >>>> >>>> ouput: >>>> >>>> elapsed time: 0.136568599 seconds (47999984 bytes allocated, 18.22% gc >>>> time) >>>> >>>> >>>> >>>> Thanks. >>>> >>>> On Wednesday, February 18, 2015 at 4:56:40 PM UTC-6, Stefan Karpinski >>>> wrote: >>>>> >>>>> How did you do the timing? >>>>> >>>>> On Wed, Feb 18, 2015 at 5:54 PM, Rohan Fernando <[email protected]> >>>>> wrote: >>>>> >>>>>> I ran both, Julia and C++, on Mac OS X 10.10.2. >>>>>> >>>>>> Is that the information you asked for? >>>>>> >>>>>> Thanks! >>>>>> >>>>>> On Wednesday, February 18, 2015 at 2:32:23 PM UTC-6, Stefan Karpinski >>>>>> wrote: >>>>>>> >>>>>>> Depends on how fast your system libm is – and how accurate. There's >>>>>>> a tradeoff. >>>>>>> >>>>>>> On Wed, Feb 18, 2015 at 3:07 PM, Rohan Fernando <[email protected]> >>>>>>> wrote: >>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> The log and exponential functions in Julia are about five times >>>>>>>> slower than in C++. Is this reasonable? >>>>>>>> >>>>>>> >>>>>>> >>>>> >>>
