Add slice swap benchmark.
Adds a benchmark of various different ways of swapping the elements of
two slices, including several implementations in C.
Change-Id: I7ff490aefee6edfe5d7630b851278ce1fc385e8c
diff --git a/c/swap.c b/c/swap.c
new file mode 100644
index 0000000..657ae9d
--- /dev/null
+++ b/c/swap.c
@@ -0,0 +1,27 @@
+#include "swap.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+void swap_loop(char* restrict a, char* restrict b, size_t len) {
+ while (len--) {
+ char tmp = *a;
+ *a++ = *b;
+ *b++ = tmp;
+ };
+}
+
+void swap_malloc(char* restrict a, char* restrict b, size_t len) {
+ char *temp = malloc(len);
+ memmove(temp,a,len);
+ memmove(a,b,len);
+ memmove(b,temp,len);
+ free(temp);
+}
+
+void swap_alloca(char* restrict a, char* restrict b, size_t len) {
+ char temp[len];
+ memmove(temp,a,len);
+ memmove(a,b,len);
+ memmove(b,temp,len);
+}