Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- // using std::vector
- #include <cstdint>
- // using std::uint8_t
- #include <cstring>
- // using std::memcpy
- #include <numeric>
- // using std::iota
- #include <chrono>
- // using std::chrono::high_resolution_clock, std::chrono::microseconds
- #include <iostream>
- // using std::cout
- int main()
- {
- typedef std::chrono::high_resolution_clock clock_t;
- typedef std::chrono::microseconds us_t;
- #define IN_LEN 4096
- #ifdef LARGE_BLOCKS
- const std::size_t increments = 512;
- #elif defined(SMALL_BLOCKS)
- const std::size_t increments = 4;
- #elif defined(BLOCKSIZE)
- # if IN_LEN % BLOCKSIZE == 0
- const std::size_t increments = BLOCKSIZE;
- # else
- # error 4096 has to be divisible by BLOCKSIZE
- # endif
- #else
- # error Define LARGE_BLOCKS, SMALL_BLOCKS or a custom size with BLOCKSIZE
- #endif
- const std::size_t iterations = 1024 * 1024;
- std::uint8_t* input = new std::uint8_t[IN_LEN];
- std::iota(input, input + IN_LEN, std::uint8_t(0));
- clock_t::time_point t0 = clock_t::now();
- for(std::size_t i = 0; i < iterations; ++i) {
- #ifdef INSERT
- std::vector<std::uint8_t> out;
- out.reserve(IN_LEN);
- std::uint8_t* const end = input + IN_LEN;
- for(std::uint8_t* pos = input, *next; pos != end; pos = next) {
- next = pos + increments;
- out.insert(out.end(), pos, next);
- }
- #elif defined(MEMCPY)
- std::vector<std::uint8_t> out(IN_LEN);
- std::uint8_t* const end = input + IN_LEN;
- std::uint8_t* write_pos = &out[0];
- for(std::uint8_t* pos = input; pos != end; pos += increments) {
- write_pos =
- static_cast<std::uint8_t*>(std::memcpy(write_pos, pos, increments));
- write_pos += increments;
- }
- #else
- # error Define INSERT or MEMCPY
- #endif
- }
- clock_t::duration t = clock_t::now() - t0;
- us_t us = std::chrono::duration_cast<us_t>(t);
- std::cout << us.count() << " us\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment