Speed in Matlab, C and Java
Here, you will find the code for the demonstrations
from the class of (2/5/04).
I created 4 files:
Here is a transcript of my session with Matlab in which
I tested these out on my workstation:
- speedTest.m --
a matlab loop that adds random numbers,
- speedTest2.m --
a faster version of the matlab code.
The reason that it is faster is that loops are very
slow in matlab, but vector-based operations
are very fast.
In my tests on my workstation, this one was
almost as good as C.
- mex_speedTest.c --
C code to do the same thing.
See the file for compilation instructions.
It also provides at least one example of how
C and matlab can communicate.
- SpeedTest.java --
the Java code.
You compile this with javac to create SpeedTest.class,
just like you would any other piece of Java code.
>> n = 10^6;
>> tic; x = speedTest(n); toc
>> tic; x = speedTest2(n); toc
>> tic; x = mex_speedTest(n); toc
>> s = SpeedTest
Methods for class SpeedTest:
SpeedTest getClass notify sumRand wait
equals hashCode notifyAll toString
>> tic; s.sumRand(n); toc
Last modified: Thu Feb 5 21:25:59 EST 2004