t=zeros(1,4);
n=1000; % size of matrices
it=2; % average results over XX trails
for ii=1:it,
% random inputs
% John's rejected solution
tic;
n1=sum(diag(A*B'));
t(1)=t(1)+toc;
% element-wise solution
tic;
n2=sum(sum(A.*B));
t(2)=t(2)+toc;
% MOST efficient solution - using vector product
tic;
n3=A(:)'*B(:);
t(3)=t(3)+toc;
% using trace
tic;
n4=trace(A*B');
t(4)=t(4)+toc;
% make sure everything is correct
end;
t./it
dD16ZXJvcygxLDQpOwpuPTEwMDA7ICUgc2l6ZSBvZiBtYXRyaWNlcwppdD0yOyAlIGF2ZXJhZ2UgcmVzdWx0cyBvdmVyIFhYIHRyYWlscwpmb3IgaWk9MTppdCwgCiAgICAlIHJhbmRvbSBpbnB1dHMKICAgIEE9cmFuZChuKTsKICAgIEI9cmFuZChuKTsgCiAgICAlIEpvaG4ncyByZWplY3RlZCBzb2x1dGlvbgogICAgdGljOyAKICAgIG4xPXN1bShkaWFnKEEqQicpKTsKICAgIHQoMSk9dCgxKSt0b2M7CiAgICAlIGVsZW1lbnQtd2lzZSBzb2x1dGlvbgogICAgdGljOwogICAgbjI9c3VtKHN1bShBLipCKSk7CiAgICB0KDIpPXQoMikrdG9jOwogICAgJSBNT1NUIGVmZmljaWVudCBzb2x1dGlvbiAtIHVzaW5nIHZlY3RvciBwcm9kdWN0CiAgICB0aWM7CiAgICBuMz1BKDopJypCKDopOwogICAgdCgzKT10KDMpK3RvYzsKICAgICUgdXNpbmcgdHJhY2UKICAgIHRpYzsKICAgIG40PXRyYWNlKEEqQicpOwogICAgdCg0KT10KDQpK3RvYzsKICAgICUgbWFrZSBzdXJlIGV2ZXJ5dGhpbmcgaXMgY29ycmVjdAogICAgYXNzZXJ0KGFicyhuMS1uMik8MWUtOCAmJiBhYnMobjMtbjQpPDFlLTggJiYgYWJzKG4xLW40KTwxZS04KTsKZW5kOwp0Li9pdA==