From: James Tursa on
"us " <us(a)neurol.unizh.ch> wrote in message <hpdbvo$ik8$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hpdatu$351$1(a)fred.mathworks.com>...
> > "us " <us(a)neurol.unizh.ch> wrote in message <hpd468$gr4$1(a)fred.mathworks.com>...
> > >
> > > for i=1:nt
> > > xr(i,1)=sum(x2);
> > > end
> > > r=all((xr-xr(1))==0)
> > > % r = 1
> >
> > You need to be very careful with tests like this in MATLAB. Sometimes the JIT accelerator will recognize that certain calculations don't change inside a loop and move that calculation outside the loop, effectively torpedoing your intended test. You may need to wrap such function tests inside another function to coax JIT into not optimizing it out of the loop. I know this happens with matrix multiply operations as I had a heck of a time (and still do) designing meaningful timing tests for my mtimesx FEX submission.
> >
> > James Tursa
>
> james
> quite(!) interesting and never heard/thought of it...
> for the example shown, it would mean that the jit-engine recognizes that
>
> sum(x2)
>
> is always the same and computes it only once BEFORE the loop starts(?)...
> is this documented(?)...
>
> urs

Maybe. An optimizer certainly *could* do that, but I don't know the JIT details for what it does specifically with regards to loops and function calls. I don't know that the JIT details are documented anywhere. I think MATLAB regards this as proprietary and subject to change from release to release and they don't want the community to write code that depends too much on a particular JIT feature, so they don't publish very many details. For the sum() function in particular, I don't know if the JIT will move it out of the loop ... I would have to run some tests. I just know for sure that the mtimes operation *is* effectively moved out of a loop under certain circumstances. The way I got around this in my mtimesx timing tests (drove me nuts until I tracked the cause down btw) was to change one of the operand elements inside the loop so the JIT couldn't move the operation outside the loop.

James Tursa
 | 
Pages: 1
Prev: Recursion in Matlab functions
Next: GUI to .NET