Here's an interesting question for the coders among us.
I have two flavors of a simulation I'm running. The algorithm is identical other than running one using a For Each() Loop and the other running Parallel Assign. The For Each loop consistently takes much less time to do the same calculation set as the Parallel Assign, even with all 8 of my cores maxed out.

Anybody have thoughts as to why that might be? Code for each version of the simulation is below:
Parallel Assign Version
// JSL Code for Simulation of Harmonized Compendial Dissolution Criteria USP <711>
// Simulation function for USP dissolution - three stage test
// reps = number of simulated lots for simulation
// mean = the process mean
// sd = the process standard deviation
// Q = value as specified in the mongraph for the product
//
// Summary Matrix Key
// ==================
// 1 "PassS1"
// 2 "PassS2"
// 3 "PassS3"
// 4 "#<Q s1="">= Q & sum_matrix[i, 7] == 0,
sum_matrix[i, 2] = 1;
sum_matrix[i, 12] = 1;
sum_matrix[i, 14] = 0;
,
sum_matrix[i, 2] = 0;
If( sum_matrix[i, 7] <= 2 & sum_matrix[i, 8] == 0,
sum_matrix[i, 14] = 1,
sum_matrix[i, 14] = 0;
sum_matrix[i, 12] = 0;
);
)
);
// Stage 3
If( sum_matrix[i, 14] == 1,
If( sum_matrix[i, 9] >= Q & sum_matrix[i, 10] <= 2 & sum_matrix[i, 11] == 0,
sum_matrix[i, 3] = 1;
sum_matrix[i, 12] = 1;
,
sum_matrix[i, 3] = 0;
sum_matrix[i, 12] = 0;
)
);
);
// return the value
Mean( sum_matrix[0, 12] );
)},
simMat[a, b] = simDissoTest( repN, 80 + cs[1] + cs[3] * (b - 1), a * rs[3] )
);
stop = Tick Seconds();
time = stopWatch( start, stop, "min" );
Show( time, ntests );</Q>
For Each () Version
// JSL Code for Simulation of Harmonized Compendial Dissolution Criteria USP <711>
// Simulation function for USP dissolution - three stage test
// reps = number of simulated lots for simulation
// mean = the process mean
// sd = the process standard deviation
// Q = value as specified in the mongraph for the product
//
// Summary Matrix Key
// ==================
// 1 "PassS1"
// 2 "PassS2"
// 3 "PassS3"
// 4 "#<Q s1="">= Q & sum_matrix[i, 7] == 0,
sum_matrix[i, 2] = 1;
sum_matrix[i, 12] = 1;
sum_matrix[i, 14] = 0;
,
sum_matrix[i, 2] = 0;
If( sum_matrix[i, 7] <= 2 & sum_matrix[i, 8] == 0,
sum_matrix[i, 14] = 1,
sum_matrix[i, 14] = 0;
sum_matrix[i, 12] = 0;
);
)
);
// Stage 3
If( sum_matrix[i, 14] == 1,
If( sum_matrix[i, 9] >= Q & sum_matrix[i, 10] <= 2 & sum_matrix[i, 11] == 0,
sum_matrix[i, 3] = 1;
sum_matrix[i, 12] = 1;
,
sum_matrix[i, 3] = 0;
sum_matrix[i, 12] = 0;
)
);
);
// return the value
Mean( sum_matrix[0, 12] );
);
//---------------------------------------------------------------------------------
// Parallelized Simulation
//---------------------------------------------------------------------------------
// define the size of the matrix ans the step size
rows = vecSize( 0.1, 12, 0.1 );
rowShape = [0.1, 12, 0.1];
cols = vecSize( -1, 10, 0.5 );
colShape = [-1, 10, 0.5];
reps = 100;
ntests = rows * cols;
//create the Parallel test matrix
simMat = J( rows, cols, Empty() );
// run the simulation (testing scalability)
//New Window( "test", lb = Lineup Box( N Col( 2 ) ) );
/*For Each( {repN, i}, {1, 50, 100, 500},
start = Tick Seconds();
For Each( {v, {i, j}}, simMat,
simMat[i, j] = simDissoTest( repN, 80 + colShape[1] + colShape[3] * (j - 1), i * rowShape[3] )
);
stop = Tick Seconds();
time = stopWatch( start, stop, "min" );
Show( repN, time, ntests );
//lb << Append( V List Box( Text Box( "Number of reps: " || Char( repN ) ), New Heat Image( simMat ) ) );
);*/
// run the simulation
start = Tick Seconds();
For Each( {v, {i, j}}, simMat,
simMat[i, j] = simDissoTest( reps, 80 + colShape[1] + colShape[3] * (j - 1), i * rowShape[3] )
);
stop = Tick Seconds();
time = stopWatch( start, stop, "min" );
Show( reps, time, ntests );
// make the data table from the matrix
tallMatrix = J( ntests, 3, Empty() );
k = 1; // an indexer
For Each( {v, {i, j}}, simMat,
tallMatrix[k, 1] = 80 + colShape[1] + colShape[3] * (j - 1);
tallMatrix[k, 2] = i * rowShape[3];
tallMatrix[k, 3] = v;
k++;
);
dt = As Table( tallMatrix );
Column( dt, 1 ) << Set Name( "Mean" );
Column( dt, 2 ) << Set Name( "Std Dev" );
Column( dt, 3 ) << Set Name( "Pass Rate" );
dt << New Script(
"Graph",
Graph Builder(
Size( 742, 817 ),
Show Control Panel( 0 ),
Fit to Window,
Variables( X( :Mean ), Y( :Std Dev ), Color( :Pass Rate ) ),
Elements( Contour( X, Y, Legend( 6 ), Number of Levels( 5 ), Smoothness( 0.146217717445672 ) ) ),
SendToReport(
Dispatch(
{},
"Mean",
ScaleBox,
{Min( 79.0036098075223 ), Max( 89.9788461538462 ), Inc( 2 ), Minor Ticks( 1 )}
),
Dispatch(
{},
"Std Dev",
ScaleBox,
{Min( 0.0888646043293861 ), Max( 11.971158234127 ), Inc( 2 ), Minor Ticks( 1 )}
),
Dispatch(
{},
"400",
ScaleBox,
{Legend Model(
6,
Properties(
0,
{gradient(
{Color Theme( "Green Yellow Red"(1) ), Scale Values( [0.8 .] ),
Reverse Gradient( 1 )}
)},
Item ID( "Pass Rate", 1 )
)
)}
)
)
)
);
dt << Run Script("Graph");
disclaimer;</Q>Hi Mike,
I confess to only a quick scan of the code, but are you specifying 1000 reps for the parallel version, and only 100 reps for the For Each version? (Although the function defaults to 100 reps, it looks like you're pushing 1000 to the parallel version due to line 58.)
When I used lines 58 and 321 to specify 1, 10 and 100 reps (I've not the patience for 1000, sorry), the results were similar at 1 and 10, but parallel was quicker at 100.
This seems reasonable to me, since any gains in efficiency will only overcome the overhead of the parallel thread setup once the task becomes sufficiently large.
-Brady