In R2008b Linux 64, the first (single-line) loop takes a bit less than half of
the time of the same code written out across multiple lines if the argument 0
is given, and a bit more than half of the multiline version if the argument 1
is given.
My impression from previous discussions is that the single-line version was
more difficult for the JIT to deal with; I would thus expect the single-line
version to be either the same magnitude of time or slower than the multi-line
version ?
I recall reading that 2010a had significant JIT improvements so perhaps the
2010 releases have very different time scales than I observed.
Also, I would appreciate if you could run the same routine under the profiler.
When I do that, the timing for the single-line version stays about the same,
but the timing for the multi-line version increases by about a factor of 10. I
would have expected the profiler to be implemented in such a way that relative
timing were not affected (at least not by much)
To run, invoke
testif(0)
or
testif(1)
function testif(v)
thisstate = double(v);
disp('double');
c = 0;tic;for k = 1:20000;if thisstate; c=c+1;end;end;toc
thisstate = double(v);
disp('double multiline');
c = 0;
tic;
for k = 1:20000
if thisstate
c=c+1;
end
end
toc
end
(I noticed this oddity while I was timing which data type was the fastest to
test in "if" statements. Answer: double is almost always the fastest, though
logical occasionally is faster. uint8 is measurably slower to test.)
>> testif(0)
double
Elapsed time is 0.000820 seconds.
double multiline
Elapsed time is 0.000225 seconds.
>> testif(1)
double
Elapsed time is 0.001135 seconds.
double multiline
Elapsed time is 0.000299 seconds.
>>
Thanks, Bruno; those are quite different than what I see 2008b. It will be
interesting to see which release first fixed the issue.
>> testif(0)
double
Elapsed time is 0.000447 seconds.
double multiline
Elapsed time is 0.000171 seconds.
>> testif(1)
double
Elapsed time is 0.000785 seconds.
double multiline
Elapsed time is 0.000178 seconds.
>> profile on
>> testif(1)
double
Elapsed time is 0.000747 seconds.
double multiline
Elapsed time is 0.010959 seconds.
>> profile on
>> testif(0)
double
Elapsed time is 0.000394 seconds.
double multiline
Elapsed time is 0.004756 seconds.
/per
Interesting test you have come up with. I have run it against all the
MATLAB releases I have at hand, but you have me curious, so I might see if
I can fill in some gaps in the list of releases. I have tabulated results
in rows to save some space. They are in the order of testif(0) double,
double multiline, testif(1) double, double multiline.
The results for the Linux versions seem very similar to each other, so I
think I will need to look back a bit further in the release history.
The Solaris results show quite some change across the releases, and
certainly not always for the better. Interesting to me is that MATLAB 6.5
seems to be about as good as it gets under Solaris. It has always been my
preferred version, except when I have a need for 64 bit, in which case
5.3.1 was good until R2009a finally offered 64 bit capabilities once more.
I have run all of these with the -nodesktop flag, except MATLAB 5.3.1 under
Solaris, and the second run of MATLAB 6.5 under Solaris, where I used
-nojvm for a bit of extra speed.
At this point I have results for the following:
=======================
Linux:
Ubuntu 10.04 running under VirtualBox on a Sun Ultra 24 (Intel Quad
core 3.0GHz). Host OS is Solaris 10 05/09.
=======================
MATLAB 7.6.0.324 (R2008a)
=======================
0.000357 0.000107 0.000439 0.000106
>> profile on
0.000321 0.000647 0.000390 0.001242
MATLAB 7.7.0.471 (R2008b)
=======================
0.000357 0.000085 0.000423 0.000094
>> profile on
0.000342 0.000653 0.000423 0.001255
MATLAB 7.8.0.347 (R2009a) 64-bit (glnxa64)
=======================
0.000328 0.000091 0.000392 0.000091
>> profile on
0.000314 0.000653 0.000392 0.001249
=======================
SunFire 280R, 2 x 1.2GHz (SPARC), Solaris 10 05/09
=======================
MATLAB 5.3.1.29215a (R11.1)
=======================
0.0145 0.0160 0.0369 0.0420
>> profile on
0.0133 0.0813 0.0446 0.1793
MATLAB 6.5.0.180913a Release 13
=======================
0.0022 0.0020 0.0026 0.0026
>> profile on
0.0020 0.1203 0.0027 0.2356
MATLAB 7.0.4.352 (R14) Service Pack 2
=======================
0.001695 0.002505 0.002184 0.003948
>> profile on
0.001562 0.156719 0.002157 0.311997
MATLAB 7.1.0.183 (R14) Service Pack 3
=======================
0.001550 0.002724 0.002031 0.003865
>> profile on
0.001563 0.157677 0.002110 0.312204
MATLAB 7.7.0.471 (R2008b)
=======================
0.002062 0.003321 0.003177 0.005704
>> profile on
0.002085 0.009706 0.003282 0.018474
MATLAB 7.8.0.347 (R2009a) 64-bit (sol64)
=======================
0.002083 0.003464 0.003395 0.005903
>> profile on
0.002106 0.010328 0.003248 0.018446
MATLAB 6.5.0.180913a Release 13, -nojvm
=======================
0.0022 0.0020 0.0026 0.0026
>> profile on
0.0020 0.0696 0.0026 0.1374
--
Dr Tristram J. Scott
Energy Consultant