| 1 | // |
|---|---|
| 2 | // Benchmark.cpp |
| 3 | // |
| 4 | // This sample shows a benchmark of various mutex implementations. |
| 5 | // |
| 6 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/Mutex.h" |
| 14 | #include "Poco/Stopwatch.h" |
| 15 | #include <iostream> |
| 16 | #include <iomanip> |
| 17 | |
| 18 | template<typename Mtx> |
| 19 | void Benchmark(Mtx& mtx, std::string const& label) |
| 20 | { |
| 21 | Poco::Stopwatch sw; |
| 22 | sw.start(); |
| 23 | |
| 24 | const int LOOP_COUNT = 1000000000; |
| 25 | |
| 26 | mtx.lock(); |
| 27 | |
| 28 | for (int i = 0 ; i < LOOP_COUNT ; ++i) |
| 29 | { |
| 30 | mtx.unlock(); |
| 31 | mtx.lock(); |
| 32 | } |
| 33 | |
| 34 | mtx.unlock(); |
| 35 | |
| 36 | sw.stop(); |
| 37 | |
| 38 | std::cout << label << ' ' << sw.elapsed() << " [us]"<< std::endl; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | int main(int argc, char** argv) |
| 43 | { |
| 44 | { |
| 45 | Poco::NullMutex mtx; |
| 46 | Benchmark(mtx, "NullMutex"); |
| 47 | } |
| 48 | |
| 49 | { |
| 50 | Poco::Mutex mtx(Poco::Mutex::MUTEX_RECURSIVE); |
| 51 | Benchmark(mtx, "Mutex(MUTEX_RECURSIVE)"); |
| 52 | } |
| 53 | |
| 54 | { |
| 55 | Poco::Mutex mtx(Poco::Mutex::MUTEX_NONRECURSIVE); |
| 56 | Benchmark(mtx, "Mutex(MUTEX_NONRECURSIVE)"); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | Poco::FastMutex mtx; |
| 61 | Benchmark(mtx, "FastMutex"); |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 |