This commit is contained in:
16 changed files with 296 additions and 143 deletions

View File

@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0020 NEW)
find_package(PIP REQUIRED)
add_executable(mutex mutex.cpp)
target_include_directories(mutex PUBLIC ${PIP_INCLUDES})
target_link_libraries(mutex ${PIP_LIBRARY} ${PIP_CONCURRENT_LIBRARY})
add_executable(mutex_multithread mutex_multithread.cpp)
target_include_directories(mutex_multithread PUBLIC ${PIP_INCLUDES})
target_link_libraries(mutex_multithread ${PIP_LIBRARY} ${PIP_CONCURRENT_LIBRARY})
add_executable(vectors vectors.cpp)
target_include_directories(vectors PUBLIC ${PIP_INCLUDES})
target_link_libraries(vectors ${PIP_LIBRARY})

49
experiments/pip/mutex.cpp Normal file
View 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";
}

View File

@@ -0,0 +1,60 @@
#include <pimutex.h>
#include <atomic>
#include <future>
#include <picout.h>
#include <vector>
float check_performance(std::function<long(long&)> test_function) {
long k = 0;
std::vector<std::future<float>> perThreadPerformance;
for (int j = 0; j < 6; ++j) {
auto future = std::async(std::launch::async, [=, &k](){
auto start = std::chrono::high_resolution_clock::now();
while (test_function(k) < 1000 * 1000) { }
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
});
perThreadPerformance.push_back(std::move(future));
}
float res = 0;
for (int j = 0; j < perThreadPerformance.size(); ++j) {
res += perThreadPerformance[j].get();
}
return res / perThreadPerformance.size();
}
int main() {
PIMutex piMutex;
auto piMutexPerformance = check_performance([&piMutex](long& k){
piMutex.lock();
int i = 0; while (i < 1000) { i++; }
long res = ++k;
piMutex.unlock();
return res;
});
piCout << "piMutex:" << piMutexPerformance << "ms";
std::mutex stdMutex;
auto stdMutexPerformance = check_performance([&stdMutex](long& k){
stdMutex.lock();
int i = 0; while (i < 1000) { i++; }
long res = ++k;
stdMutex.unlock();
return res;
});
piCout << "stdMutex:" << stdMutexPerformance << "ms";
std::atomic_flag stdAtomic = ATOMIC_FLAG_INIT;
auto stdAtomicPerformance = check_performance([&stdAtomic](long& k){
while(stdAtomic.test_and_set(std::memory_order_acquire)) {
std::this_thread::yield();
}
int i = 0; while (i < 1000) { i++; }
long res = ++k;
stdAtomic.clear(std::memory_order_release);
return res;
});
piCout << "stdAtomic:" << stdAtomicPerformance << "ms";
}

View File

@@ -0,0 +1,50 @@
#include <vector>
#include <pivector.h>
#include <chrono>
#include <memory>
template<typename Func>
float check_performance(Func test_function) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 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() {
float piVectorPerformance = check_performance([](){
PIVector<int> piVector;
for (int i = 0; i < 1000; ++i) {
piVector.push_back(i);
}
});
piCout << "piVector without preallocation:" << piVectorPerformance << "ms";
float stdVectorPerformance = check_performance([](){
std::vector<int> stdVector;
for (int i = 0; i < 1000; ++i) {
stdVector.push_back(i);
}
});
piCout << "stdVector without preallocation:" << stdVectorPerformance << "ms";
std::unique_ptr<std::vector<int> > stdVector(new std::vector<int>(1000));
stdVectorPerformance = check_performance(std::bind([](std::unique_ptr<std::vector<int> >& stdVector){
for (int i = 0; i < 1000; ++i) {
stdVector->at(i) = i;
}
}, std::move(stdVector)));
piCout << "stdVector with preallocation:" << stdVectorPerformance << "ms";
std::unique_ptr<int> array(new int[1000]);
float arrayPerformance = check_performance(std::bind([](std::unique_ptr<int>& array){
for (int i = 0; i < 1000; ++i) {
array.get()[i] = i;
}
}, std::move(array)));
piCout << "array:" << arrayPerformance << "ms";
return 0;
}