58 lines
1.7 KiB
C++
58 lines
1.7 KiB
C++
#include <pimutex.h>
|
|
#include <piconditionlock.h>
|
|
#include <atomic>
|
|
#include <future>
|
|
#include <picout.h>
|
|
|
|
std::future<float> check_performance(std::function<void()> test_function) {
|
|
return std::async(std::launch::deferred, [=](){
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
for (int i = 0; i < 1000 * 1000; ++i) {
|
|
test_function();
|
|
}
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
|
|
});
|
|
}
|
|
|
|
int main() {
|
|
auto withoutSyncPerformance = check_performance([](){
|
|
int i = 0; while (i < 1000) i++;
|
|
});
|
|
piCout << "without synchronization:" << withoutSyncPerformance.get() << "ms";
|
|
|
|
|
|
PIMutex piMutex;
|
|
auto piMutexPerformance = check_performance([&piMutex](){
|
|
piMutex.lock();
|
|
int i = 0; while (i < 1000) i++;
|
|
piMutex.unlock();
|
|
});
|
|
piCout << "piMutex:" << piMutexPerformance.get() << "ms";
|
|
|
|
PIConditionLock piConditionLock;
|
|
auto piConditionLockPerformance = check_performance([&piConditionLock](){
|
|
piConditionLock.lock();
|
|
int i = 0; while (i < 1000) i++;
|
|
piConditionLock.unlock();
|
|
});
|
|
piCout << "piConditionLock:" << piConditionLockPerformance.get() << "ms";
|
|
|
|
std::mutex stdMutex;
|
|
auto stdMutexPerformance = check_performance([&stdMutex](){
|
|
stdMutex.lock();
|
|
int i = 0; while (i < 1000) i++;
|
|
stdMutex.unlock();
|
|
});
|
|
piCout << "stdMutex:" << stdMutexPerformance.get() << "ms";
|
|
|
|
std::atomic_flag stdAtomic = ATOMIC_FLAG_INIT;
|
|
auto stdAtomicPerformance = check_performance([&stdAtomic](){
|
|
while(stdAtomic.test_and_set(std::memory_order_acquire)) {
|
|
std::this_thread::yield();
|
|
}
|
|
int i = 0; while (i < 1000) i++;
|
|
stdAtomic.clear(std::memory_order_release);
|
|
});
|
|
piCout << "stdAtomic:" << stdAtomicPerformance.get() << "ms";
|
|
} |