Bonita Montero
unread,Nov 27, 2022, 7:29:49 AM11/27/22You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__unix__)
#include <unistd.h>
#endif
#include <iostream>
#include <vector>
#include <atomic>
#include <thread>
#include <string_view>
#include <memory>
#include <chrono>
using namespace std;
using namespace chrono;
int main()
{
#if defined(_WIN32)
SYSTEM_INFO si;
GetSystemInfo( &si );
ptrdiff_t pageSize = si.dwPageSize;
#elif defined(__unix__)
ptrdiff_t pageSize = sysconf( _SC_PAGESIZE );
#endif
constexpr size_t
N_BOUNDARIES = 100,
ROUNDS = 1'000'000;
size_t range = pageSize * (N_BOUNDARIES + 2);
vector<char> vc( range + pageSize - 1 );
char *begin = (char *)(((size_t)to_address( vc.begin() ) + pageSize - 1
& -pageSize) + pageSize);
string_view view( begin, begin + N_BOUNDARIES * pageSize );
constexpr ptrdiff_t HALF_SIZE_T = sizeof(size_t) / 2;
static struct
{
ptrdiff_t offset;
char const *what;
} const offsets[3] =
{
{ -HALF_SIZE_T, "page-boundary", },
{ 0, "aligned" },
{ HALF_SIZE_T, "unaligned", }
};
for( auto const &offset : offsets )
{
auto start = high_resolution_clock::now();
ptrdiff_t off = offset.offset;
for( size_t r = ROUNDS; r--; )
for( string_view::iterator it = view.begin(), end = view.end(); ; )
{
(void)((atomic<size_t> &)to_address( it )[off]).load(
memory_order_relaxed );
// before increment because of iterator-debugging
if( it == end ) [[unlikely]]
break;
it += pageSize;
}
double ns = duration_cast<nanoseconds>( high_resolution_clock::now() -
start ).count() / ((double)N_BOUNDARIES * ROUNDS);
cout << offset.what << ": " << ns << endl;
}
}