Skip to content

Commit

Permalink
Add benchmarks for basic_string::erase
Browse files Browse the repository at this point in the history
Reviewers: EricWF

Subscribers: christof, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D73740
  • Loading branch information
martijnvels committed Feb 19, 2020
1 parent 7ff1f55 commit c4b8c3d
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions libcxx/benchmarks/string.bench.cpp
Expand Up @@ -308,6 +308,58 @@ struct StringAssignAsciiz {
}
};

template <class Length, class Opaque>
struct StringEraseToEnd {
static void run(benchmark::State& state) {
constexpr bool opaque = Opaque{} == Opacity::Opaque;
constexpr int kNumStrings = 4 << 10;
std::string strings[kNumStrings];
const int mid = makeString(Length()).size() / 2;
while (state.KeepRunningBatch(kNumStrings)) {
state.PauseTiming();
for (int i = 0; i < kNumStrings; ++i) {
strings[i] = makeString(Length());
}
benchmark::DoNotOptimize(strings);
state.ResumeTiming();
for (int i = 0; i < kNumStrings; ++i) {
strings[i].erase(maybeOpaque(mid, opaque),
maybeOpaque(std::string::npos, opaque));
}
}
}

static std::string name() {
return "BM_StringEraseToEnd" + Length::name() + Opaque::name();
}
};

template <class Length, class Opaque>
struct StringEraseWithMove {
static void run(benchmark::State& state) {
constexpr bool opaque = Opaque{} == Opacity::Opaque;
constexpr int kNumStrings = 4 << 10;
std::string strings[kNumStrings];
const int n = makeString(Length()).size() / 2;
const int pos = n / 2;
while (state.KeepRunningBatch(kNumStrings)) {
state.PauseTiming();
for (int i = 0; i < kNumStrings; ++i) {
strings[i] = makeString(Length());
}
benchmark::DoNotOptimize(strings);
state.ResumeTiming();
for (int i = 0; i < kNumStrings; ++i) {
strings[i].erase(maybeOpaque(pos, opaque), maybeOpaque(n, opaque));
}
}
}

static std::string name() {
return "BM_StringEraseWithMove" + Length::name() + Opaque::name();
}
};

template <class Opaque>
struct StringAssignAsciizMix {
static void run(benchmark::State& state) {
Expand Down Expand Up @@ -556,6 +608,8 @@ int main(int argc, char** argv) {
makeCartesianProductBenchmark<StringDestroy, AllLengths>();
makeCartesianProductBenchmark<StringResizeDefaultInit, AllLengths,
AllOpacity>();
makeCartesianProductBenchmark<StringEraseToEnd, AllLengths, AllOpacity>();
makeCartesianProductBenchmark<StringEraseWithMove, AllLengths, AllOpacity>();
makeCartesianProductBenchmark<StringRelational, AllRelations, AllLengths,
AllLengths, AllDiffTypes>();
makeCartesianProductBenchmark<StringRelationalLiteral, AllRelations,
Expand Down

0 comments on commit c4b8c3d

Please sign in to comment.