'Determine the optimal batch size'

def compute_batch_size(self):

    old_batch_size = self._effective_batch_size
    batch_duration = self._smoothed_batch_duration
    if ((batch_duration > 0) and (batch_duration < self.MIN_IDEAL_BATCH_DURATION)):
        ideal_batch_size = int(((old_batch_size * self.MIN_IDEAL_BATCH_DURATION) / batch_duration))
        batch_size = max((2 * ideal_batch_size), 1)
        self._effective_batch_size = batch_size
        if (self.parallel.verbose >= 10):
            self.parallel._print('Batch    computation    too    fast    (%.4fs.)    Setting    batch_size=%d.', (batch_duration, batch_size))
    elif ((batch_duration > self.MAX_IDEAL_BATCH_DURATION) and (old_batch_size >= 2)):
        batch_size = (old_batch_size // 2)
        self._effective_batch_size = batch_size
        if (self.parallel.verbose >= 10):
            self.parallel._print('Batch    computation    too    slow    (%.4fs.)    Setting    batch_size=%d.', (batch_duration, batch_size))
    else:
        batch_size = old_batch_size
    if (batch_size != old_batch_size):
        self._smoothed_batch_duration = 0
    return batch_size
