Array Rotation, Timing Tests
March 9, 2018
We have been looking at Section 2.3 of Jon Bentley’s book Programming Pearls in the last two exercises, and have implemented his “juggling” and “block swap” algorithms. Bentley also discusses a third algorithm, which he calls the “reversal” algorithm, and which we implemented several years ago. Bentley goes on to give timing comparisons between the three algorithms.
Your task is to generate timing comparisons similar to Bentley’s, to see what happens with your system, your language and your compiler. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
I compiled the code with Gambit, added declarations
(declare (standard-bindings)
(extended-bindings)
(block)
(fixnum)
(not safe))
except for
(define (timing rotate vec dist)
(declare (generic))
(let ((start (cpu-time)))
(rotate vec dist)
(- (cpu-time) start)))
and got times
which is closer to Bentley’s results
Here’s a solution in C.
The output times follow the code.
The swapping approach worked fastest of my implementations.
I was able to improve the speed of the juggle approach by using subtraction to calculate the remainder instead of using the modulus operator.
I also added code to validate that my rotation implementations are working correctly.
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void rotate_juggling(int* array, size_t n, size_t i) { if (n == 0) return; size_t n_moved = 0; size_t start_idx = 0; while (1) { int tmp = array[start_idx]; size_t dest_idx = start_idx; size_t source_idx = start_idx + i; if (source_idx >= n) source_idx -= n; while (1) { ++n_moved; if (source_idx == start_idx) { array[dest_idx] = tmp; break; } array[dest_idx] = array[source_idx]; dest_idx = source_idx; source_idx = source_idx + i; if (source_idx >= n) source_idx -= n; } if (n_moved == n) break; ++start_idx; } } static void swap(int* a1, int* a2, size_t n) { for (size_t i = 0; i < n; ++i) { int tmp = a1[i]; a1[i] = a2[i]; a2[i] = tmp; } } void rotate_swap(int* array, size_t n, size_t i) { i %= n; while (1) { if (n <= 1 || i == 0) break; if (i == n - i) { swap(array, array + i, i); break; } else if (i < n - i) { swap(array, array + n - i, i); n -= i; } else { swap(array, array + i, n - i); array += n - i; size_t n_ = n; n = i; i -= n_ - i; } } } static void reverse(int* array, size_t n) { for (size_t i = 0; i < n / 2; ++i) { int tmp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = tmp; } } void rotate_reverse(int* array, size_t n, size_t i) { reverse(array, i); reverse(array + i, n - i); reverse(array, n); } clock_t calc_rotate_time(void (*rotate) (int*, size_t, size_t), int* array, size_t n, size_t i, size_t iterations) { int* array_ = malloc(sizeof(int) * n); clock_t total = 0; for (size_t iter = 0; iter < iterations; ++iter) { memcpy(array_, array, n * sizeof(int)); clock_t start = clock(); rotate(array, n, i); clock_t end = clock(); total += end - start; } free(array_); return (double)total; } int main(void) { size_t iterations = 1000; size_t n = 10000; int* array = malloc(n * sizeof(int)); for (size_t idx = 0; idx < n; ++idx) { array[idx] = idx; } size_t increment = 1000; void (*rotate_functions[])(int*,size_t,size_t) = { rotate_juggling, rotate_swap, rotate_reverse }; char* rotate_function_names[] = {"juggle", "swap", "reverse"}; size_t n_rotate_functions = sizeof(rotate_function_names) / sizeof(char*); printf("dist"); for (size_t i = 0; i < n_rotate_functions; ++i) { printf("\t%s", rotate_function_names[i]); } printf("\n"); for (size_t distance = increment; distance < n; distance += increment) { printf("%zu", distance); for (size_t i = 0; i < n_rotate_functions; ++i) { void (*rotate_function)(int*,size_t,size_t) = rotate_functions[i]; // *************************************** // * Validate Rotation // *************************************** int* rotated = malloc(n * sizeof(int)); memcpy(rotated, array, n * sizeof(int)); rotate_function(rotated, n, distance); for (size_t k = 0; k < n; ++k) { assert(rotated[k] == array[(k+distance)%n]); } free(rotated); // *************************************** // * Time Rotation // *************************************** clock_t time = calc_rotate_time( rotate_function, array, n, distance, iterations); printf("\t%lu", time); } printf("\n"); } free(array); return EXIT_SUCCESS; }Output (-O0):
Output (-O2):
My calc_rotate_time function casts the result to a double in the return statement. That should be removed. An earlier implementation was calculating seconds, and the cast to double was there to bypass integer division. Removing the cast to double won’t have a substantive effect on the numbers reported above.