Skip to content

Instantly share code, notes, and snippets.

@alanduan
Created June 10, 2020 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanduan/5a0d441fb2b37edad1f332c3a3e46f63 to your computer and use it in GitHub Desktop.
Save alanduan/5a0d441fb2b37edad1f332c3a3e46f63 to your computer and use it in GitHub Desktop.
time how fast we can write to shared memory
#include <fstream>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
int main()
{
/*std::string io_file_path{ "c:\\working\\io.bin" };
std::ofstream iofile{ io_file_path, std::ios::out | std::ios::binary };
if (iofile.is_open())
{
iofile << '\x00';
iofile.close();
}*/
boost::interprocess::file_mapping fm{ "c:\\working\\io.bin", boost::interprocess::read_write };
boost::interprocess::mapped_region mr(fm, boost::interprocess::read_write, 0, 1);
uint8_t& io_value = *reinterpret_cast<uint8_t*>(mr.get_address());
std::clock_t c_start = std::clock();
for (int i = 0; i < 1'000'000'000; ++i) {
io_value++;
}
std::clock_t c_end = std::clock();
auto time_elapsed_ms = 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC;
std::cout << "CPU time used: " << time_elapsed_ms << " ms\n";
// on my PC it takes about 800ms to 1000ms to write 10^9 times. about 1GHz
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment