From 966745e79b871c576b5ebdabea526b7acd3a4251 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Tue, 17 Jan 2023 14:52:00 +0000 Subject: Puzzle 16: Attempt to speed up the "reverse" function Even though we change from 3 small memcpy()s per 2 items, to 1 small memcpy() per item plus 1 big memcpy(), it goes slower? (On part 1 of the puzzle with a 40 minute limit, the timing goes from 6.6s to 6.8s, average of 5 runs.) Leave commented out for now. --- 16.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/16.c b/16.c index 15e9671..bb565bf 100644 --- a/16.c +++ b/16.c @@ -34,6 +34,16 @@ qreverse(void * base, size_t nmemb, size_t size) if (nmemb < 2) return; +#if 0 // This goes slower?!? + if (nmemb < BUFSIZ / size) { + char tmp[nmemb * size]; + memcpy(tmp, base, nmemb * size); + for (i = 0; i < nmemb; ++i) + memcpy(base + i * size, tmp + (nmemb - 1 - i) * size, size); + return; + } +#endif + for (i = 0, j = nmemb - 1; i < j; ++i, --j) qswap(base, size, i, j); } -- cgit v1.2.1