t=zeros(1,4);
n=10000; % size of matrices
it=2; % average results over XX trails
for ii=1:it, 
    % random inputs
    A=rand(n);
    B=rand(n); 
    % 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
    assert(abs(n1-n2)<1e-8 && abs(n3-n4)<1e-8 && abs(n1-n4)<1e-8);
end;
t./it