|
| 1 | +// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*- |
| 2 | +// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi |
| 3 | +// |
| 4 | +// Copyright 2024 Justine Alexandra Roberts Tunney |
| 5 | +// |
| 6 | +// Permission to use, copy, modify, and/or distribute this software for |
| 7 | +// any purpose with or without fee is hereby granted, provided that the |
| 8 | +// above copyright notice and this permission notice appear in all copies. |
| 9 | +// |
| 10 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 11 | +// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 12 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
| 13 | +// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 14 | +// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 15 | +// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 16 | +// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 | +// PERFORMANCE OF THIS SOFTWARE. |
| 18 | + |
| 19 | +#include "ctl/string.h" |
| 20 | + |
| 21 | +#include <__utility/move.h> |
| 22 | + |
| 23 | +#include "libc/calls/struct/timespec.h" |
| 24 | +#include "libc/runtime/runtime.h" |
| 25 | +#include "libc/stdio/stdio.h" |
| 26 | +#include "libc/str/str.h" |
| 27 | + |
| 28 | +#define BENCH(ITERATIONS, WORK_PER_RUN, CODE) \ |
| 29 | + do { \ |
| 30 | + struct timespec start = timespec_real(); \ |
| 31 | + for (int __i = 0; __i < ITERATIONS; ++__i) { \ |
| 32 | + asm volatile("" ::: "memory"); \ |
| 33 | + CODE; \ |
| 34 | + } \ |
| 35 | + long long work = (WORK_PER_RUN) * (ITERATIONS); \ |
| 36 | + double nanos = \ |
| 37 | + (timespec_tonanos(timespec_sub(timespec_real(), start)) + work - \ |
| 38 | + 1) / \ |
| 39 | + (double)work; \ |
| 40 | + printf("%10g ns %2dx %s\n", nanos, (ITERATIONS), #CODE); \ |
| 41 | + } while (0) |
| 42 | + |
| 43 | +const char* big_c = "aaaaaaaaaaaaaaaaaaaaaaaa"; |
| 44 | +const char* small_c = "aaaaaaaaaaaaaaaaaaaaaaa"; |
| 45 | + |
| 46 | +int |
| 47 | +main() |
| 48 | +{ |
| 49 | + const ctl::string_view big(big_c), small(small_c); |
| 50 | + |
| 51 | + BENCH(10000000, 1, { |
| 52 | + ctl::string s; |
| 53 | + s.append("hello "); |
| 54 | + s.append("world"); |
| 55 | + }); |
| 56 | + |
| 57 | + BENCH(1000000, 8, { |
| 58 | + ctl::string s; |
| 59 | + for (int i = 0; i < 8; ++i) { |
| 60 | + s.append('a'); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + BENCH(1000000, 16, { |
| 65 | + ctl::string s; |
| 66 | + for (int i = 0; i < 16; ++i) { |
| 67 | + s.append('a'); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + BENCH(1000000, 23, { |
| 72 | + ctl::string s; |
| 73 | + for (int i = 0; i < 23; ++i) { |
| 74 | + s.append('a'); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + BENCH(1000000, 24, { |
| 79 | + ctl::string s; |
| 80 | + for (int i = 0; i < 24; ++i) { |
| 81 | + s.append('a'); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + BENCH(1000000, 32, { |
| 86 | + ctl::string s; |
| 87 | + for (int i = 0; i < 32; ++i) { |
| 88 | + s.append('a'); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + BENCH(1000000, 1, { ctl::string s(small_c); }); |
| 93 | + |
| 94 | + BENCH(1000000, 1, { ctl::string s(small); }); |
| 95 | + |
| 96 | + { |
| 97 | + ctl::string small_copy("hello world"); |
| 98 | + BENCH(1000000, 1, { ctl::string s2(small_copy); }); |
| 99 | + } |
| 100 | + |
| 101 | + BENCH(1000000, 1, { |
| 102 | + ctl::string s(small); |
| 103 | + ctl::string s2(std::move(s)); |
| 104 | + }); |
| 105 | + |
| 106 | + BENCH(1000000, 1, { |
| 107 | + ctl::string s(small); |
| 108 | + ctl::string s2(s); |
| 109 | + }); |
| 110 | + |
| 111 | + BENCH(1000000, 1, { ctl::string s(big_c); }); |
| 112 | + |
| 113 | + BENCH(1000000, 1, { ctl::string s(big); }); |
| 114 | + |
| 115 | + { |
| 116 | + ctl::string big_copy(big); |
| 117 | + BENCH(1000000, 1, { ctl::string s2(big_copy); }); |
| 118 | + } |
| 119 | + |
| 120 | + BENCH(1000000, 1, { |
| 121 | + ctl::string s(big); |
| 122 | + ctl::string s2(std::move(s)); |
| 123 | + }); |
| 124 | + |
| 125 | + BENCH(1000000, 1, { |
| 126 | + ctl::string s(big); |
| 127 | + ctl::string s2(s); |
| 128 | + }); |
| 129 | + |
| 130 | + BENCH(1000000, 1, { ctl::string s(23, 'a'); }); |
| 131 | + |
| 132 | + BENCH(1000000, 1, { ctl::string s(24, 'a'); }); |
| 133 | + |
| 134 | + { |
| 135 | + ctl::string s(5, 'a'); |
| 136 | + BENCH(1000000, 1, { ctl::string_view s2(s); }); |
| 137 | + } |
| 138 | + |
| 139 | + { |
| 140 | + ctl::string big_trunc(48, 'a'); |
| 141 | + big_trunc.resize(4); |
| 142 | + BENCH(1000000, 1, { ctl::string s(big_trunc); }); |
| 143 | + } |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
0 commit comments