softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

Interesting finding #65

Open Despair2000 opened 5 years ago

Despair2000 commented 5 years ago

I found something interesting performance wise that I want to share.

Obviously MATLAB is pretty slow when calling Java-methods in dot-notation but a simple modification of the syntax increases the speed 3-fold!

Look at the following example:

First a call to isEmpty in dot-notation tic; for i=1:100000 execDetails.buffer.isEmpty; end; toc Elapsed time is 1.887634 seconds.

Now the exactly same code just with another notation of the function call tic; for i=1:100000 isEmpty(execDetails.buffer); end; toc Elapsed time is 0.629575 seconds.

When using IB4m we make many calls to Java-methods so this little change can result in a quite significant performance improvement.

Despair2000 commented 5 years ago

For the interested reader I found a very useful post on stackoverflow related to this matter. :-)

https://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong

ecpgieicg commented 5 years ago

Can confirm:

contract = com.ib.client.Contract();
contract.symbol("VIX");contract.secType("OPT");contract.exchange("SMART");contract.currency("USD");contract.lastTradeDateOrContractMonth("20191119");contract.strike(30);contract.right("Call");
[buf,lh] = TWS.initBufferForEvent(TWS.Events.HISTORICALDATA);
session.eClientSocket.reqHistoricalData(101,contract,'20191029 16:00:00','1 W','1 min','BID',1,1,false,[]);
% after some wait
>> timeit(@()buf.size)
ans =
   1.3239e-05
>> timeit(@()size(buf))
ans =
   3.7409e-06
ecpgieicg commented 5 years ago

@softwarespartan

If object method calls in Matlab have a lot of overhead, would it be better to use function syntax instead in collection2cell since there are a few object method calls for each single tick?

Like

iter = iterator(collection);

% init
out = cell(size(collection),1);  indx = 1;

% iterate over the collection and build output
while hasNext(iter); out{indx} = next(iter); indx = indx + 1; end

instead of

iter = collection.iterator();

% init
out = cell(collection.size,1);  indx = 1;

% iterate over the collection and build output
while iter.hasNext(); out{indx} = iter.next(); indx = indx + 1; end

?

softwarespartan commented 5 years ago

There are many ways to skin a cat ...

If you really need speed (and memory management), MATLAB is not the right platform.

IB4m allows to easily develop and forward test "mathy" financial algorithms with IB TWS directly in MATLAB. If really worried about those microseconds then should be using C++. And, if really want to be trading against market depth I and II then IB is probably not the right broker/data provider :)

Sure, can speed things up with some syntax changes (a little less readable though) but hopefully those speedups are in the noise.

ecpgieicg commented 5 years ago

Understand.

I don't intend to engage in high frequency tradings either. I am only concerned with smooth running when requesting historical or real time data on a continuous basis.

Off-topic, I actually had good experiences with memory/speed in Matlab for matrix manipulations. (Disclaimer: big matrices need to be split into blocks; although that's not usually bothersome.) I never experienced issue with Matlab's infamous garbage collection either. It removes every local variable after a function call. And since functions that need to share/modify variables can be nested under another function, that's pretty good for static computational tasks. Matlab is usually as fast as or faster than my own scripts in c++ calling those old fortran libraries. And ofc, Matlab saves a lot of development time.

Despair2000 commented 5 years ago

I also agree that MATLAB is not the right platform if one needs speed but switching from dot-notation to function syntax is such a small thing and IMO improves readability.

Like @ecpgieicg I'm not interested in HFT but just smooth running application and in functions that are called thousands of times like for example processnotification.m or collection2cell it really makes sense.