summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-12 22:47:55 +0100
committerTyge Løvset <[email protected]>2023-02-12 23:20:18 +0100
commit7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d (patch)
tree681d1894d917bc2fe244375298ea40f736c18e18 /misc
parent9904a7ea36f9e4f45d7e41e409ed23ad22821e8a (diff)
downloadSTC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.tar.gz
STC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.zip
Fairly large update before release 4.1, cleaning up docs and some minor additions.
Diffstat (limited to 'misc')
-rw-r--r--misc/benchmarks/build_all.sh4
-rw-r--r--misc/benchmarks/various/cbits_benchmark.cpp (renamed from misc/benchmarks/misc/cbits_benchmark.cpp)0
-rw-r--r--misc/benchmarks/various/csort_bench.c61
-rw-r--r--misc/benchmarks/various/cspan_bench.c (renamed from misc/benchmarks/misc/cspan_bench.c)0
-rw-r--r--misc/benchmarks/various/names.txt (renamed from misc/benchmarks/misc/names.txt)0
-rw-r--r--misc/benchmarks/various/prng_bench.cpp (renamed from misc/benchmarks/misc/prng_bench.cpp)0
-rw-r--r--misc/benchmarks/various/rust_cmap.c (renamed from misc/benchmarks/misc/rust_cmap.c)0
-rw-r--r--misc/benchmarks/various/rust_hashmap.rs (renamed from misc/benchmarks/misc/rust_hashmap.rs)0
-rw-r--r--misc/benchmarks/various/sso_bench.cpp (renamed from misc/benchmarks/misc/sso_bench.cpp)0
-rw-r--r--misc/benchmarks/various/string_bench_STC.cpp (renamed from misc/benchmarks/misc/string_bench_STC.cpp)0
-rw-r--r--misc/benchmarks/various/string_bench_STD.cpp (renamed from misc/benchmarks/misc/string_bench_STD.cpp)0
-rw-r--r--misc/examples/prime.c9
-rw-r--r--misc/examples/sort.c64
13 files changed, 67 insertions, 71 deletions
diff --git a/misc/benchmarks/build_all.sh b/misc/benchmarks/build_all.sh
index 869559ba..d055bd2e 100644
--- a/misc/benchmarks/build_all.sh
+++ b/misc/benchmarks/build_all.sh
@@ -15,12 +15,12 @@ if [ ! -z "$1" ] ; then
cc=$@
fi
if [ $run = 0 ] ; then
- for i in *.cpp misc/*.c* picobench/*.cpp plotbench/*.cpp ; do
+ for i in *.cpp various/*.c* picobench/*.cpp plotbench/*.cpp ; do
echo $cc -I../include $i -o $(basename -s .cpp $i).exe
$cc -I../include $i -o $(basename -s .cpp $i).exe
done
else
- for i in misc/*.c* picobench/*.cpp ; do
+ for i in various/*.c* picobench/*.cpp ; do
echo $cc -O3 -I../include $i
$cc -O3 -I../include $i
if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi
diff --git a/misc/benchmarks/misc/cbits_benchmark.cpp b/misc/benchmarks/various/cbits_benchmark.cpp
index dd709db1..dd709db1 100644
--- a/misc/benchmarks/misc/cbits_benchmark.cpp
+++ b/misc/benchmarks/various/cbits_benchmark.cpp
diff --git a/misc/benchmarks/various/csort_bench.c b/misc/benchmarks/various/csort_bench.c
new file mode 100644
index 00000000..97885eb8
--- /dev/null
+++ b/misc/benchmarks/various/csort_bench.c
@@ -0,0 +1,61 @@
+// Generic Quicksort in C, performs as fast as c++ std::sort().
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+#ifdef __cplusplus
+ #include <algorithm>
+#endif
+#define i_val int
+#include <stc/algo/csort.h>
+
+#define ROTL(d,bits) ((d<<(bits)) | (d>>(8*sizeof(d)-(bits))))
+uint64_t random(uint64_t s[3]) {
+ uint64_t xp = s[0], yp = s[1], zp = s[2];
+ s[0] = 15241094284759029579u * zp;
+ s[1] = yp - xp; s[1] = ROTL(s[1], 12);
+ s[2] = zp - yp; s[2] = ROTL(s[2], 44);
+ return xp;
+}
+
+void testsort(int *a, int size, const char *desc) {
+ clock_t t = clock();
+#ifdef __cplusplus
+ { printf("std::sort: "); std::sort(a, a + size); }
+#else
+ { printf("stc_sort: "); csort_int(a, size); }
+#endif
+ t = clock() - t;
+
+ printf("time: %.1fms, n: %d, %s\n",
+ (double)t*1000.0/CLOCKS_PER_SEC, size, desc);
+}
+
+
+int main(int argc, char *argv[]) {
+ size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000;
+ uint64_t s[3] = {123456789, 3456789123, 789123456};
+
+ int32_t *a = (int32_t*)malloc(sizeof(*a) * size);
+ if (!a) return -1;
+
+ for (i = 0; i < size; i++)
+ a[i] = random(s) & (1U << 30) - 1;
+ testsort(a, size, "random");
+ for (i = 0; i < 20; i++)
+ printf(" %d", (int)a[i]);
+ puts("");
+ for (i = 0; i < size; i++)
+ a[i] = i;
+ testsort(a, size, "sorted");
+ for (i = 0; i < size; i++)
+ a[i] = size - i;
+ testsort(a, size, "reverse sorted");
+ for (i = 0; i < size; i++)
+ a[i] = 126735;
+ testsort(a, size, "constant");
+ for (i = 0; i < size; i++)
+ a[i] = i + 1;
+ a[size - 1] = 0;
+ testsort(a, size, "rotated");
+ free(a);
+}
diff --git a/misc/benchmarks/misc/cspan_bench.c b/misc/benchmarks/various/cspan_bench.c
index 589df13a..589df13a 100644
--- a/misc/benchmarks/misc/cspan_bench.c
+++ b/misc/benchmarks/various/cspan_bench.c
diff --git a/misc/benchmarks/misc/names.txt b/misc/benchmarks/various/names.txt
index 561acbbf..561acbbf 100644
--- a/misc/benchmarks/misc/names.txt
+++ b/misc/benchmarks/various/names.txt
diff --git a/misc/benchmarks/misc/prng_bench.cpp b/misc/benchmarks/various/prng_bench.cpp
index 6f4e0e47..6f4e0e47 100644
--- a/misc/benchmarks/misc/prng_bench.cpp
+++ b/misc/benchmarks/various/prng_bench.cpp
diff --git a/misc/benchmarks/misc/rust_cmap.c b/misc/benchmarks/various/rust_cmap.c
index 83b7dd19..83b7dd19 100644
--- a/misc/benchmarks/misc/rust_cmap.c
+++ b/misc/benchmarks/various/rust_cmap.c
diff --git a/misc/benchmarks/misc/rust_hashmap.rs b/misc/benchmarks/various/rust_hashmap.rs
index 5394a7c3..5394a7c3 100644
--- a/misc/benchmarks/misc/rust_hashmap.rs
+++ b/misc/benchmarks/various/rust_hashmap.rs
diff --git a/misc/benchmarks/misc/sso_bench.cpp b/misc/benchmarks/various/sso_bench.cpp
index 0fffef7a..0fffef7a 100644
--- a/misc/benchmarks/misc/sso_bench.cpp
+++ b/misc/benchmarks/various/sso_bench.cpp
diff --git a/misc/benchmarks/misc/string_bench_STC.cpp b/misc/benchmarks/various/string_bench_STC.cpp
index ae8e4c38..ae8e4c38 100644
--- a/misc/benchmarks/misc/string_bench_STC.cpp
+++ b/misc/benchmarks/various/string_bench_STC.cpp
diff --git a/misc/benchmarks/misc/string_bench_STD.cpp b/misc/benchmarks/various/string_bench_STD.cpp
index 8bb87937..8bb87937 100644
--- a/misc/benchmarks/misc/string_bench_STD.cpp
+++ b/misc/benchmarks/various/string_bench_STD.cpp
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index 4a3b8498..1a272e78 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -27,20 +27,19 @@ cbits sieveOfEratosthenes(intptr_t n)
int main(void)
{
intptr_t n = 1000000000;
- printf("computing prime numbers up to %" c_ZI "\n", n);
+ printf("Computing prime numbers up to %" c_ZI "\n", n);
clock_t t1 = clock();
c_with (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) {
- puts("done");
intptr_t np = cbits_count(&primes);
clock_t t2 = clock();
- printf("number of primes: %" c_ZI ", time: %f\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
+ printf("Number of primes: %" c_ZI ", time: %f\n\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
puts("Show all the primes in the range [2, 1000):");
printf("2");
c_forrange (i, 3, 1000, 2)
if (cbits_test(&primes, i>>1)) printf(" %lld", i);
- puts("");
+ puts("\n");
puts("Show the last 50 primes using a temporary crange generator:");
crange R = crange_make(n - 1, 0, -2);
@@ -48,7 +47,7 @@ int main(void)
, cbits_test(&primes, *i.ref>>1)
, c_flt_take(i, 50)) {
printf("%lld ", *i.ref);
- if (i.count % 10 == 0) puts("");
+ if (c_flt_last(i) % 10 == 0) puts("");
}
}
}
diff --git a/misc/examples/sort.c b/misc/examples/sort.c
deleted file mode 100644
index 65077143..00000000
--- a/misc/examples/sort.c
+++ /dev/null
@@ -1,64 +0,0 @@
-#include <time.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stc/crandom.h>
-
-#ifdef __cplusplus
- #include <algorithm>
- typedef long long csort_elm_value;
-#else
- #define i_tag elm
- #define i_val long long
- #include <stc/algo/csort.h>
-#endif
-#define fmt_Elem "%lld"
-
-
-int testsort(csort_elm_value *a, long size, const char *desc) {
- clock_t t = clock();
-#ifdef __cplusplus
- printf("std::sort: ");
- std::sort(a, a + size);
-#else
- printf("csort: ");
- csort_elm(a, size);
-#endif
- t = clock() - t;
-
- printf("%s: %d elements sorted in %.2f ms\n",
- desc, (int)size, (float)t*1000.0f/CLOCKS_PER_SEC);
- return 0;
-}
-
-int main(int argc, char *argv[]) {
- long i, size = argc > 1 ? strtol(argv[1], NULL, 0) : 10000000;
- csort_elm_value *a = (csort_elm_value*)c_malloc(c_sizeof(*a) * size);
- if (a == NULL) return -1;
-
- for (i = 0; i < size; i++)
- a[i] = crandom() & ((1U << 28) - 1);
-
- testsort(a, size, "random");
- for (i = 0; i < 20; i++)
- printf(" " fmt_Elem, a[i]);
- puts("");
- for (i = 1; i < size; i++)
- if (a[i - 1] > a[i]) return -1;
-
- testsort(a, size, "sorted");
-
- for (i = 0; i < size; i++) a[i] = size - i;
- testsort(a, size, "reverse sorted");
-
- for (i = 0; i < size; i++) a[i] = 126735;
- testsort(a, size, "constant");
-
- for (i = 0; i < size; i++) a[i] = i + 1;
- a[size - 1] = 0;
- testsort(a, size, "rotated");
-
- for (i = 0; i < size; i++) a[i] = i % (size / 8);
- testsort(a, size, "repeated");
-
- free(a);
-}