diff options
| -rw-r--r-- | include/stc/forward.h | 20 | ||||
| -rw-r--r-- | misc/benchmarks/various/cspan_bench.c | 43 | ||||
| -rw-r--r-- | misc/examples/spans/submdspan.c | 8 |
3 files changed, 24 insertions, 47 deletions
diff --git a/include/stc/forward.h b/include/stc/forward.h index 2372a618..2fbff034 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -41,13 +41,13 @@ // csview : non-null terminated string view typedef const char csview_value; -typedef struct csview { - csview_value* buf; +typedef struct csview { + csview_value* buf; intptr_t size; } csview; -typedef union { - csview_value* ref; +typedef union { + csview_value* ref; csview chr; struct { csview chr; csview_value* end; } u8; } csview_iter; @@ -55,13 +55,13 @@ typedef union { // crawstr : null-terminated string view typedef csview_value crawstr_value; -typedef struct crawstr { - crawstr_value* str; +typedef struct crawstr { + crawstr_value* str; intptr_t size; } crawstr; -typedef union { - crawstr_value* ref; +typedef union { + crawstr_value* ref; csview chr; struct { csview chr; } u8; // [deprecated] } crawstr_iter; @@ -75,8 +75,8 @@ typedef union cstr { struct { cstr_value* data; size_t size, ncap; } lon; } cstr; -typedef union { - cstr_value* ref; +typedef union { + cstr_value* ref; csview chr; struct { csview chr; } u8; // [deprecated] } cstr_iter; diff --git a/misc/benchmarks/various/cspan_bench.c b/misc/benchmarks/various/cspan_bench.c index b5caca83..3b1c3132 100644 --- a/misc/benchmarks/various/cspan_bench.c +++ b/misc/benchmarks/various/cspan_bench.c @@ -1,3 +1,4 @@ +// ref: https://stackoverflow.com/questions/74382366/why-is-iterating-over-stdrangesviewsjoin-so-slow #define NDEBUG #include <stc/cspan.h> #include <stdio.h> @@ -11,6 +12,7 @@ enum { ny = 64, nz = 64 }; +// subspan 15x5x10: int lx = 15, ly = 10, lz = 5; int hx = 30, hy = 15, hz = 15; @@ -20,27 +22,7 @@ double Vin[nx * ny * nz]; //, 1.23; // define some slice indices for each dimension -static void MDRanges_setup(intptr_t n) -{ - double sum = 0; - clock_t t = clock(); - - for (intptr_t s = 0; s < n; ++s) - { - MD3 r_in = cspan_md(Vin, nx, ny, nz); - MD3 r_out = cspan_md(Vout, nx, ny, nz); - - r_in = cspan_slice(MD3, &r_in, {lx, hx}, {ly, hy}, {lz, hz}); - r_out = cspan_slice(MD3, &r_out, {lx, hx}, {ly, hy}, {lz, hz}); - MD3_iter i = MD3_begin(&r_in); // can be iterated "flat". - MD3_iter o = MD3_begin(&r_out); - sum += Vin[s % nx]; - } - t = clock() - t; - printf("setup: %.1f ms, %f\n", 1000.0f * t / CLOCKS_PER_SEC, sum); -} - -static void TraditionalForLoop(intptr_t n) +static void Traditional_for_loop(intptr_t n) { clock_t t = clock(); double sum = 0; @@ -57,7 +39,7 @@ static void TraditionalForLoop(intptr_t n) } } t = clock() - t; - printf("forloop: %.1f ms, %f\n", 1000.0f * t / CLOCKS_PER_SEC, sum); + printf("forloop : %.1f ms, %f\n", 1000.0f*t / CLOCKS_PER_SEC, sum); } static void MDRanges_nested_loop(intptr_t n) @@ -67,8 +49,6 @@ static void MDRanges_nested_loop(intptr_t n) MD3 r_out = cspan_md(Vout, nx, ny, nz); r_in = cspan_slice(MD3, &r_in, {lx, hx}, {ly, hy}, {lz, hz}); r_out = cspan_slice(MD3, &r_out, {lx, hx}, {ly, hy}, {lz, hz}); - - // C++23: for (auto [o, i] : std::views::zip(flat(r_out), flat(r_in))) { o = i; } double sum = 0; for (intptr_t s = 0; s < n; ++s) { @@ -76,27 +56,25 @@ static void MDRanges_nested_loop(intptr_t n) for (int y = 0; y < r_in.shape[1]; ++y) { for (int z = 0; z < r_in.shape[2]; ++z) { - double d = *cspan_at(&r_in, x, y, z); - *cspan_at(&r_out, x, y, z) += d; + double d = *cspan_at(&r_in, x,y,z); + *cspan_at(&r_out, x,y,z) += d; sum += d; } } } } t = clock() - t; - printf("nested: %.1f ms, %f\n", 1000.0f * t / CLOCKS_PER_SEC, sum); + printf("nested : %.1f ms, %f\n", 1000.0f*t / CLOCKS_PER_SEC, sum); } static void MDRanges_loop_over_joined(intptr_t n) { + clock_t t = clock(); MD3 r_in = cspan_md(Vin, nx, ny, nz); MD3 r_out = cspan_md(Vout, nx, ny, nz); r_in = cspan_slice(MD3, &r_in, {lx, hx}, {ly, hy}, {lz, hz}); r_out = cspan_slice(MD3, &r_out, {lx, hx}, {ly, hy}, {lz, hz}); - - // C++23: for (auto [o, i] : std::views::zip(flat(r_out), flat(r_in))) { o = i; } double sum = 0; - clock_t t = clock(); for (intptr_t s = 0; s < n; ++s) { MD3_iter i = MD3_begin(&r_in); @@ -109,7 +87,7 @@ static void MDRanges_loop_over_joined(intptr_t n) } } t = clock() - t; - printf("joined: %.1f ms, %f\n", 1000.0f * t / CLOCKS_PER_SEC, sum); + printf("joined : %.1f ms, %f\n", 1000.0f*t / CLOCKS_PER_SEC, sum); } int main(void) @@ -118,8 +96,7 @@ int main(void) for (int i = 0; i < nx * ny * nz; ++i) Vin[i] = i + 1.23; - MDRanges_setup(n); - TraditionalForLoop(n); + Traditional_for_loop(n); MDRanges_nested_loop(n); MDRanges_loop_over_joined(n); } diff --git a/misc/examples/spans/submdspan.c b/misc/examples/spans/submdspan.c index fa0d5762..0752dfa1 100644 --- a/misc/examples/spans/submdspan.c +++ b/misc/examples/spans/submdspan.c @@ -3,11 +3,11 @@ #include <stdio.h> #include <stc/cspan.h> -using_cspan3(span, double); // define span, span2, span3 +using_cspan3(span, double); // shorthand for defining span, span2, span3 // Set all elements of a rank-2 mdspan to zero. void zero_2d(span2 grid2d) { - c_static_assert(cspan_rank(&grid2d) == 2); + (void)c_static_assert(cspan_rank(&grid2d) == 2); for (int i = 0; i < grid2d.shape[0]; ++i) { for (int j = 0; j < grid2d.shape[1]; ++j) { *cspan_at(&grid2d, i,j) = 0; @@ -16,7 +16,7 @@ void zero_2d(span2 grid2d) { } void zero_surface(span3 grid3d) { - c_static_assert(cspan_rank(&grid3d) == 3); + (void)c_static_assert(cspan_rank(&grid3d) == 3); zero_2d(cspan_slice(span2, &grid3d, {0}, {c_ALL}, {c_ALL})); zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {0}, {c_ALL})); zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {c_ALL}, {0})); @@ -41,4 +41,4 @@ int main() { } puts(""); } -}
\ No newline at end of file +} |
