Refactor
This commit is contained in:
49
experiments/pip/mutex.cpp
Normal file
49
experiments/pip/mutex.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <pimutex.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";
|
||||
|
||||
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";
|
||||
}
|
||||
Reference in New Issue
Block a user