Skip to content

Instantly share code, notes, and snippets.

@nickelpro
Last active March 7, 2025 19:10
Show Gist options
  • Save nickelpro/1683cbdef4cfbfc3f33e66f2a7db55ae to your computer and use it in GitHub Desktop.
Save nickelpro/1683cbdef4cfbfc3f33e66f2a7db55ae to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <array>
#include <concepts>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <numeric>
#include <print>
#include <span>
#include <vector>
void a(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s);
void b(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s);
void c(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s);
void d(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s);
int main(int argc, char* argv[]) {
if(argc < 2) {
std::println("Need arg");
std::exit(EXIT_FAILURE);
}
char arg = *argv[1];
std::vector<std::uint8_t> vec;
vec.reserve(4096);
std::array<std::uint32_t, 1024> arr;
std::iota(arr.begin(), arr.end(), 0);
std::span<std::uint8_t> sp {reinterpret_cast<std::uint8_t*>(arr.data()),
sizeof(arr)};
auto bench = [&](auto func) {
for(size_t i {0}; i < 65536; ++i) {
func(vec, sp);
vec.clear();
}
std::exit(EXIT_SUCCESS);
};
switch(arg) {
case 'a':
bench(a);
case 'b':
bench(b);
case 'c':
bench(c);
case 'd':
bench(d);
}
std::println("Unknown arg");
std::exit(EXIT_FAILURE);
}
#include <algorithm>
#include <array>
#include <concepts>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <numeric>
#include <print>
#include <span>
#include <vector>
void a(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s) {
auto next = v.size();
v.resize(v.size() + s.size());
std::memcpy(v.data() + next, s.data(), s.size());
}
void b(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s) {
auto next = v.size();
v.resize(v.size() + s.size());
std::ranges::copy(s, v.begin() + next);
}
void c(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s) {
std::copy(s.begin(), s.end(), std::back_inserter(v));
}
void d(std::vector<std::uint8_t>& v, std::span<std::uint8_t> s) {
std::ranges::copy(s, std::back_inserter(v));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment