Let's say I have a batch process that executes 4 times per loop, and needs to execute a total of 9 items.
Example:
- Iteration 1 executes 4 items of 9, leaving 5 left
- Iteration 2 executes 4 times of 9, leaving 1 left
- Iteration 3 only executes 1 item because that's all that is left.
Currently I handle it with a modulus operation:
$number_of_items = 9
$number_of_executions_per_loop = 4
For $iteration = 1 to $number_of_items Step $number_of_executions_per_loop
If $number_of_items < ($iteration * $number_of_executions_per_loop) Then
$iteration_execution_count = MOD($number_of_items,$number_of_executions_per_loop)
Else
$iteration_execution_count = $number_of_executions_per_loop
EndIf
ExecuteJobsFunction($iteration_execution_count)
Next
I am wondering if there is a cleaner way to handle these types of scenarios that I see frequently. Is there a math trick that can be done to determine how many items to execute?
(n+k-1)/k
where/
is integer division. – CodesInChaos Feb 18 '15 at 07:40