#include "stdafx.h"
#include <iostream>
#include <vector>
#include <queue>
#include <ctime>
#include <chrono>
#include <type_traits>
#include <string>
#include <fstream>
using namespace std;
using namespace std::chrono;
string filepath = "D:\\Temp\\out.txt";
class Point
{
public:
int x; int y;
Point(int _x, int _y)
{
x = _x;
y = _y;
}
friend std::ostream& operator<< (std::ostream& stream, const Point& p) {
return stream << "(" << p.x << ", " << p.y << ")";
}
};
int rndrange(int r)
{
int ret = rand() % r;
return ret;
}
Point** CreateArrayPoint(int range)
{
Point** p = new Point*[range];
for (int i = 0; i < range; i++)
{
p[i] = new Point(rndrange(10), rndrange(10));
}
return p;
}
double ArrayTestPoint(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
Point** p = CreateArrayPoint(range);
for (int i = 0; i < range; i++)
{
Point r = *p[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
for (int i = 0; i < range; i++)
{
delete p[i];
}
delete[] p;
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
vector<Point> CreateVectorPoint(int range)
{
vector<Point> v;
for (int i = 0; i < range; i++)
{
v.push_back(Point(rndrange(10), rndrange(10)));
}
return v;
}
double VectorTestPoint(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
vector<Point> q = CreateVectorPoint(range);
for (int i = 0; i < q.size(); i++)
{
Point r = q[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
vector<Point> CreateRVectorPoint(int range)
{
vector<Point> v;
v.reserve(range);
for (int i = 0; i < range; i++)
{
v.push_back(Point(rndrange(10), rndrange(10)));
}
return v;
}
double RVectorTestPoint(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
vector<Point> q = CreateRVectorPoint(range);
for (int i = 0; i < q.size(); i++)
{
Point r = q[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
queue<Point> CreateQuePoint(int range)
{
std::queue<Point> dq;
for (int i = 0; i < range; i++)
{
dq.push(Point(rndrange(10), rndrange(10)));
}
return dq;
}
double QueTestPoint(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
queue<Point> q = CreateQuePoint(range);
for (int i = 0; i < q.size(); i++)
{
Point r = q.front();
q.pop();
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
int* CreateArrayInt(int range)
{
int* p = new int[range];
for (int i = 0; i < range; i++)
{
p[i] = rndrange(10);
}
return p;
}
double ArrayTestInt(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
int* p = CreateArrayInt(range);
for (int i = 0; i < range; i++)
{
int r = p[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
delete[] p;
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
vector<int> CreateVectorInt(int range)
{
vector<int> v;
for (int i = 0; i < range; i++)
{
v.push_back(rndrange(10));
}
return v;
}
double VectorTestInt(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
vector<int> q = CreateVectorInt(range);
for (int i = 0; i < q.size(); i++)
{
int r = q[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
vector<int> CreateRVectorInt(int range)
{
vector<int> v;
v.reserve(range);
for (int i = 0; i < range; i++)
{
v.push_back(rndrange(10));
}
return v;
}
double RVectorTestInt(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
vector<int> q = CreateRVectorInt(range);
for (int i = 0; i < q.size(); i++)
{
int r = q[i];
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
queue<int> CreateQueInt(int range)
{
std::queue<int> dq;
for (int i = 0; i < range; i++)
{
dq.push(rndrange(10));
}
return dq;
}
double QueTestInt(int range)
{
system_clock::time_point p1 = system_clock::now();
auto p1_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p1.time_since_epoch()).count();
queue<int> q = CreateQueInt(range);
for (int i = 0; i < q.size(); i++)
{
int r = q.front();
q.pop();
}
system_clock::time_point p2 = system_clock::now();
auto p2_ms = std::chrono::duration_cast<std::chrono::milliseconds>(p2.time_since_epoch()).count();
double d12 = ((double)p2_ms - (double)p1_ms) / 1000.0;
return d12;
}
int main()
{
queue<double> arrayTestResults;
queue<double> vectorTestResults;
queue<double> queTestResults;
queue<double> rvectorTestResults;
queue<double> arrayTestResults2;
queue<double> vectorTestResults2;
queue<double> queTestResults2;
queue<double> rvectorTestResults2;
for (int i=0; i<=10000000; i+=100000)
{
double at = ArrayTestInt(i);
double vt = VectorTestInt(i);
double qt = QueTestInt(i);
double rvt = RVectorTestInt(i);
arrayTestResults.push(at);
vectorTestResults.push(vt);
queTestResults.push(qt);
rvectorTestResults.push(rvt);
double at2 = ArrayTestPoint(i);
double vt2 = VectorTestPoint(i);
double qt2 = QueTestPoint(i);
double rvt2 = RVectorTestPoint(i);
arrayTestResults2.push(at2);
vectorTestResults2.push(vt2);
queTestResults2.push(qt2);
rvectorTestResults2.push(rvt2);
}
ofstream myfile;
myfile.open(filepath);
for (int i=0; i<10000000; i+=100000)
{
double at = arrayTestResults.front();
double vt = vectorTestResults.front();
double qt = queTestResults.front();
double rvt = rvectorTestResults.front();
double at2 = arrayTestResults2.front();
double vt2 = vectorTestResults2.front();
double qt2 = queTestResults2.front();
double rvt2 = rvectorTestResults2.front();
arrayTestResults.pop();
vectorTestResults.pop();
queTestResults.pop();
rvectorTestResults.pop();
arrayTestResults2.pop();
vectorTestResults2.pop();
queTestResults2.pop();
rvectorTestResults2.pop();
//myfile << i << "," << vt << "," << qt << endl;
myfile << i << "," << at << "," << vt << "," << qt << "," << rvt <<",";
myfile << at2 << "," << vt2 << "," << qt2 << "," << rvt2 << endl;
}
myfile.close();
cout << "tesing completed." << endl;
getchar();
return 0;
}
22 January 2019
C++ array vs vector vs queue vs vector with reserve
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment