dispatch_benchmark(3) BSD Library Functions Manual dispatch_benchmark(3)NAME
dispatch_benchmark -- Measures block execution time
SYNOPSIS
#include <dispatch/dispatch.h>
uint64_t
dispatch_benchmark(size_t count, void (^block)(void));
uint64_t
dispatch_benchmark_f(size_t count, void *context, void (*function)(void *));
DESCRIPTION
The dispatch_benchmark() function executes the given block multiple times according to the count variable and then returns the average number
of nanoseconds per execution. This function is for debugging and performance analysis work. For the best results, pass a high count value
to dispatch_benchmark(). When benchmarking concurrent code, please compare the serial version of the code against the concurrent version,
and compare the concurrent version on different classes of hardware. Please look for inflection points with various data sets and keep the
following facts in mind:
o Code bound by computational bandwidth may be inferred by proportional changes in performance as concurrency is increased.
o Code bound by memory bandwidth may be inferred by negligible changes in performance as concurrency is increased.
o Code bound by critical sections may be inferred by retrograde changes in performance as concurrency is increased.
o Intentional: locks, mutexes, and condition variables.
o Accidental: unrelated and frequently modified data on the same cache-line.
RETURN VALUE
The dispatch_benchmark() function returns the average number of nanoseconds the given block takes to execute.
SEE ALSO dispatch(3)Darwin May 1, 2009 Darwin
Check Out this Related Man Page
dispatch_apply(3) BSD Library Functions Manual dispatch_apply(3)NAME
dispatch_apply -- schedule blocks for iterative execution
SYNOPSIS
#include <dispatch/dispatch.h>
void
dispatch_apply(size_t iterations, dispatch_queue_t queue, void (^block)(size_t));
void
dispatch_apply_f(size_t iterations, dispatch_queue_t queue, void *context, void (*function)(void *, size_t));
DESCRIPTION
The dispatch_apply() function provides data-level concurrency through a "for (;;)" loop like primitive:
dispatch_queue_t the_queue = dispatch_get_concurrent_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT);
size_t iterations = 10;
// 'idx' is zero indexed, just like:
// for (idx = 0; idx < iterations; idx++)
dispatch_apply(iterations, the_queue, ^(size_t idx) {
printf("%zu
", idx);
});
Like a "for (;;)" loop, the dispatch_apply() function is synchronous. If asynchronous behavior is desired, please wrap the call to
dispatch_apply() with a call to dispatch_async() against another queue.
Sometimes, when the block passed to dispatch_apply() is simple, the use of striding can tune performance. Calculating the optimal stride is
best left to experimentation. Start with a stride of one and work upwards until the desired performance is achieved (perhaps using a power
of two search):
#define STRIDE 3
dispatch_apply(count / STRIDE, queue, ^(size_t idx) {
size_t j = idx * STRIDE;
size_t j_stop = j + STRIDE;
do {
printf("%zu
", j++);
} while (j < j_stop);
});
size_t i;
for (i = count - (count % STRIDE); i < count; i++) {
printf("%zu
", i);
}
FUNDAMENTALS
Conceptually, dispatch_apply() is a convenient wrapper around dispatch_async() and a semaphore to wait for completion. In practice, the dis-
patch library optimizes this function.
The dispatch_apply() function is a wrapper around dispatch_apply_f().
SEE ALSO dispatch(3), dispatch_async(3), dispatch_queue_create(3), dispatch_semaphore_create(3)Darwin May 1, 2009 Darwin