Last active
February 16, 2021 15:23
-
-
Save cpcloud/02e1b32ae010ebc2a5067bad42848cea to your computer and use it in GitHub Desktop.
very rough shared memory profiling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| pkgs = import <nixpkgs> { }; | |
| in | |
| with pkgs; | |
| mkShell { | |
| name = "profile-shmem"; | |
| buildInputs = [ clang_11 boost ]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <algorithm> | |
| #include <random> | |
| #include <iostream> | |
| #include <chrono> | |
| #include <cstdlib> | |
| #include <boost/interprocess/mapped_region.hpp> | |
| #include <boost/interprocess/shared_memory_object.hpp> | |
| namespace shm = boost::interprocess; | |
| int main(int argc, const char* argv[]) { | |
| std::random_device rd; | |
| std::mt19937 rng(rd()); | |
| std::uniform_int_distribution<uint8_t> dist(0, 254); | |
| size_t bytes = 5000000; | |
| std::vector<uint8_t> data(bytes, 0); | |
| size_t nruns = 5; | |
| using unit = std::chrono::microseconds; | |
| static const char* unit_name = "us"; | |
| std::vector<unit> points; | |
| points.reserve(nruns); | |
| { | |
| shm::shared_memory_object shm_obj(shm::open_or_create,"shared_memory", shm::read_write); | |
| shm::mapped_region region(shm_obj, shm::read_write, 0, bytes); | |
| shm_obj.truncate(bytes); | |
| auto address = reinterpret_cast<uint8_t*>(region.get_address()); | |
| for (size_t run = 0; run < nruns; ++run) { | |
| std::generate(address, address + bytes, [&dist, &rng]() { return dist(rng); }); | |
| auto start = std::chrono::steady_clock::now(); | |
| std::copy(address, address + bytes, data.data()); | |
| auto end = std::chrono::steady_clock::now(); | |
| points.push_back(std::chrono::duration_cast<unit>(end - start)); | |
| } | |
| shm::shared_memory_object::remove("shared_memory"); | |
| } | |
| using limits_ssize_t = std::numeric_limits<ssize_t>; | |
| auto avg = std::accumulate(points.cbegin(), points.cend(), unit(0)) / nruns; | |
| auto min = std::accumulate(points.cbegin(), points.cend(), unit(limits_ssize_t::max()), [](auto a, auto b){return std::min(a, b);}); | |
| auto max = std::accumulate(points.cbegin(), points.cend(), unit(limits_ssize_t::min()), [](auto a, auto b){return std::max(a, b);}); | |
| std::cout << "avg: " << avg.count() << " " << unit_name << std::endl; | |
| std::cout << "min: " << min.count() << " " << unit_name << std::endl; | |
| std::cout << "max: " << max.count() << " " << unit_name << std::endl; | |
| return 0; | |
| } |
Author
Author
Times:
avg: 190 us
min: 167 us
max: 262 us
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enter a nix shell:
Compile with