The Performance of Simulating Yahtzee

Help!
The function below has 3 nested functions which attempt to count the number of "Yahtzees" (all 5 dice being equal) in different ways. The output of timeit() is typically around 0.17 secs for compare(), 0.85 secs for diffAll(), and 1.20 secs for compareAll().
I am at loss on how to explain the performance of the functions below. My only thought is that the fastest function (compare()) has the fewest named function calls. Though by that logic, diffAll() should be slowest.
function yahtzeeTest()
rolls = randi(6,5,5e7,'int8');
timeit(@compare)
timeit(@diffAll)
timeit(@compareAll)
function compare()
count = nnz(rolls(1,:) == rolls(2,:) & ...
rolls(2,:) == rolls(3,:) & ...
rolls(3,:) == rolls(4,:) & ...
rolls(4,:) == rolls(5,:));
end
function diffAll()
count = nnz(all(diff(rolls,1,1)==0,1));
end
function compareAll()
count = nnz(all(rolls(1:4,:) == rolls(2:5,:),1));
end
end

Answers (2)

Accessing rolls(1:2,:) is a lot slower than accessing trolls(:,1:2) where trolls = rolls.'
See attached for various experiments.

1 Comment

Walter,
Your experiments were thorough, thank you for your time.
On my machine the {compare(), compare2(), tcompare(), and tcompare2()} group are consistently faster than the corresponding compareAll() version, which includes my original and Jan's version. It seems like the call to all() is the only difference and must account for the change in execution time. I'm curious if you agree.
Though I want the compareAll() versions to be the "right" way because it is more simple to code, and is even somewhat intuitive if you understand the way "==" behaves.

Sign in to comment.

all(rolls(1:4,:) == rolls(2:5,:))
creates a [4 x N] logical matrix, and a [1 x N] logical vector in all, while
rolls(1,:) == rolls(2,:) & ...
rolls(2,:) == rolls(3,:) & ...
rolls(3,:) == rolls(4,:) & ...
rolls(4,:) == rolls(5,:)
created 5 logical vectors a [1 x N]: 4 vectors for the ==, 1 vector as output of the 4 && operations. The first seems to be cheaper, or the all() operation is responsible to the delay.
Maybe a comparison with C is interesting:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int8_T *R1, *R2, *R3, *R4, *R5, *Rf;
int64_T count = 0;
if (!mxIsInt8(prhs[0]) || mxGetM(prhs[0]) != 5) {
mexErrMsgIdAndTxt("JSimon:compareX:BadInput",
"Input must be a [5 x N] UINT8 matrix");
}
R1 = (int8_T *) mxGetData(prhs[0]);
R2 = R1 + 1;
R3 = R2 + 1;
R4 = R3 + 1;
R5 = R4 + 1;
Rf = R1 + mxGetNumberOfElements(prhs[0]);
while (R1 < Rf) {
count += (*R1 == *R2) && (*R1 == *R3) && (*R1 == *R4) && (*R1 == *R5);
R1 += 5;
R2 = R1 + 1;
R3 = R2 + 1;
R4 = R3 + 1;
R5 = R4 + 1;
}
plhs[0] = mxCreateDoubleScalar((double) count);
}
timeit(@() compare(rolls))
0.347954922374622
timeit(@() compareMex(rolls))
0.205107332791311
By the way: It would be nice, if timeit replies times with a resolution of milli- or microseconds. 0.1 femto-seconds are funny only.

Categories

Find more on Word games in Help Center and File Exchange

Asked:

on 21 Aug 2017

Answered:

Jan
on 22 Aug 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!