1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
| #include <future>
#include <shared_mutex>
#include "boost/smart_ptr/detail/spinlock.hpp"
volatile int nvalue = 0;
int loop_nolock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
if (inc)
++nvalue;
else
--nvalue;
}
return limit;
}
volatile std::atomic_int avalue = 0;
int loop_atomiclock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
if (inc)
++avalue;
else
--avalue;
}
return limit;
}
volatile LONG ivalue = 0;
int loop_interlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
if (inc)
::InterlockedIncrement(&ivalue);
else
::InterlockedDecrement(&ivalue);
}
return limit;
}
std::mutex mlck;
volatile int mvalue = 0;
int loop_mutexlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<std::mutex> guard(mlck);
if (inc)
++mvalue;
else
--mvalue;
}
return limit;
}
std::shared_mutex smlck;
volatile int smvalue = 0;
int loop_shmutexlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<std::shared_mutex> guard(smlck);
if (inc)
++smvalue;
else
--smvalue;
}
return limit;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa904937(v=vs.85).aspx
class win_slimRWlock
{
public:
void lock()
{
::AcquireSRWLockExclusive(&srw);
}
void unlock()
{
::ReleaseSRWLockExclusive(&srw);
}
private:
SRWLOCK srw SRWLOCK_INIT;
} slck;
volatile int svalue = 0;
int loop_win_slimRWlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<win_slimRWlock> guard(slck);
if (inc)
++svalue;
else
--svalue;
}
return limit;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85).aspx
class win_CriticalSectionlock
{
public:
void lock()
{
::EnterCriticalSection(&cs);
}
void unlock()
{
::LeaveCriticalSection(&cs);
}
win_CriticalSectionlock()
{
::InitializeCriticalSection(&cs);//AndSpinCount(&cs, 1000);
}
~win_CriticalSectionlock()
{
::DeleteCriticalSection(&cs);
}
private:
CRITICAL_SECTION cs;
} cslck;
volatile int csvalue = 0;
int loop_win_CriticalSectionlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<win_CriticalSectionlock> guard(cslck);
if (inc)
++csvalue;
else
--csvalue;
}
return limit;
}
//http://preshing.com/20120226/roll-your-own-lightweight-mutex/
class win_Benaphorelock
{
public:
void lock()
{
if (::_InterlockedIncrement(&m_counter) > 1) // x86/64 guarantees acquire semantics
::WaitForSingleObject(m_semaphore, INFINITE);
}
void unlock()
{
if (::_InterlockedDecrement(&m_counter) > 0) // x86/64 guarantees release semantics
::ReleaseSemaphore(m_semaphore, 1, NULL);
}
win_Benaphorelock()
{
m_counter = 0;
m_semaphore = ::CreateSemaphore(NULL, 0, 1, NULL);
}
~win_Benaphorelock()
{
::CloseHandle(m_semaphore);
}
private:
LONG m_counter;
HANDLE m_semaphore;
} phlck;
volatile int phvalue = 0;
int loop_win_Benaphorelock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<win_Benaphorelock> guard(phlck);
if (inc)
++phvalue;
else
--phvalue;
}
return limit;
}
//http://anki3d.org/spinlock/
class custom_spinlock
{
public:
void lock()
{
while(lck.test_and_set(std::memory_order_acquire))
{}
}
void unlock()
{
lck.clear(std::memory_order_release);
}
private:
std::atomic_flag lck ATOMIC_FLAG_INIT;
} clck;
volatile int cvalue = 0;
int loop_customspinlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<custom_spinlock> guard(clck);
if (inc)
++cvalue;
else
--cvalue;
}
return limit;
}
boost::detail::spinlock blck;
volatile int bvalue = 0;
int loop_boostspinlock(bool inc, int limit)
{
//std::_tcout << "Started " << inc << " " << limit << std::endl;
for (int i = 0; i < limit; ++i)
{
std::lock_guard<boost::detail::spinlock> guard(blck);
if (inc)
++bvalue;
else
--bvalue;
}
return limit;
}
void foo_main()
{
//inspiration from http://demin.ws/blog/english/2012/05/05/atomic-spinlock-mutex/
#ifdef _DEBUG
int const lolimit = 18000000;
int const hilimit = 20000000;
#else
int const lolimit = 180000000;
int const hilimit = 200000000;
#endif
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
std::chrono::duration<double> elapsed_seconds;
std::_tcout << _T("\nloop_nolock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_nolock, true, hilimit));
loop_nolock(false, lolimit);
f.wait();
std::_tcout << nvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_nolock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_atomiclock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_atomiclock, true, hilimit));
loop_atomiclock(false, lolimit);
f.wait();
std::_tcout << avalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_atomiclock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_interlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_interlock, true, hilimit));
loop_interlock(false, lolimit);
f.wait();
std::_tcout << ivalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_interlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_mutexlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_mutexlock, true, hilimit));
loop_mutexlock(false, lolimit);
f.wait();
std::_tcout << mvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_mutexlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_shmutexlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_shmutexlock, true, hilimit));
loop_shmutexlock(false, lolimit);
f.wait();
std::_tcout << smvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_shmutexlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_customspinlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_customspinlock, true, hilimit));
loop_customspinlock(false, lolimit);
f.wait();
std::_tcout << cvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_customspinlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_boostspinlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_boostspinlock, true, hilimit));
loop_boostspinlock(false, lolimit);
f.wait();
std::_tcout << bvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_boostspinlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_win_slimRWlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_win_slimRWlock, true, hilimit));
loop_win_slimRWlock(false, lolimit);
f.wait();
std::_tcout << svalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_win_slimRWlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_win_slimRWlock sequential\n");
start = std::chrono::high_resolution_clock::now();
{
svalue = 0;
loop_win_slimRWlock(true, hilimit);
loop_win_slimRWlock(false, lolimit);
std::_tcout << svalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_win_slimRWlock sequential elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_win_CriticalSectionlock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_win_CriticalSectionlock, true, hilimit));
loop_win_CriticalSectionlock(false, lolimit);
f.wait();
std::_tcout << csvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_win_CriticalSectionlock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_win_CriticalSectionlock sequential\n");
start = std::chrono::high_resolution_clock::now();
{
csvalue = 0;
loop_win_CriticalSectionlock(true, hilimit);
loop_win_CriticalSectionlock(false, lolimit);
std::_tcout << csvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_win_CriticalSectionlock sequential elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit+hilimit)) << _T("ns)\n");
std::_tcout << _T("\nloop_win_Benaphorelock\n");
start = std::chrono::high_resolution_clock::now();
{
auto f = std::async(std::launch::async, std::bind(loop_win_Benaphorelock, true, hilimit/100));
loop_win_Benaphorelock(false, lolimit/100);
f.wait();
std::_tcout << phvalue << std::endl;
}
end = std::chrono::high_resolution_clock::now();
elapsed_seconds = end-start;
std::_tcout << _T("loop_win_Benaphorelock elapsed time: ") << elapsed_seconds.count() << _T("s (") << (elapsed_seconds.count()*1e9/(lolimit/100+hilimit/100)) << _T("ns)\n");
} |
Partager