51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
#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;
|
|
}
|