void f0(int n, std::vector<int> v);
void f1(int n, std::vector<int> v);
void f2(int n, std::vector<int> v);
void f3(int n, std::vector<int> v);
void f4(int n, int arr[], int size);
void f5(int n, int arr[], int size);
void f0(int n, std::vector<int> v);
void f1(int n, std::vector<int> v);
void f2(int n, std::vector<int> v);
void f3(int n, std::vector<int> v);
void f4(int n, int arr[], int size);
void f5(int n, int arr[], int size);
To copy to clipboard, switch view to plain text mode
//**********
double d;
//auto v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "vector\n";
//**********
auto start_time = std::chrono::high_resolution_clock::now();
//for (int t = 0; t < 100; t++)
for (auto t = 0; t != 100; ++t)
{
for (int s = 0; s < 1000; s++)
f0 (1000, v);
}
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
d = std::chrono::duration_cast<std::chrono::microseconds>(time).count();
std::cout << "f0 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time6 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
//for (auto t = 0; t != 100; ++t)
{
for (int s = 0; s < 1000; s++)
f0 (1000, v);
}
auto end_time6 = std::chrono::high_resolution_clock::now();
auto time6 = end_time6 - start_time6;
d = std::chrono::duration_cast<std::chrono::microseconds>(time6).count();
std::cout << "f6 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time1 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f1 (1000, v);
}
auto end_time1 = std::chrono::high_resolution_clock::now();
auto time1 = end_time1 - start_time1;
d = std::chrono::duration_cast<std::chrono::microseconds>(time1).count();
std::cout << "f1 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time2 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f2 (1000, v);
}
auto end_time2 = std::chrono::high_resolution_clock::now();
auto time2 = end_time2 - start_time2;
d = std::chrono::duration_cast<std::chrono::microseconds>(time2).count();
std::cout << "f2 took " << (d / 100) << " microseconds reverse.\n";
//**********
auto start_time3 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f3 (1000, v);
}
auto end_time3 = std::chrono::high_resolution_clock::now();
auto time3 = end_time3 - start_time3;
d = std::chrono::duration_cast<std::chrono::microseconds>(time3).count();
std::cout << "f3 took " << (d / 100)
<< " microseconds.\n";
//**********
int a[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "\narray\n";
//**********
auto start_time4 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f4 (1000, a, (sizeof(a)/sizeof(a[0])));
}
auto end_time4 = std::chrono::high_resolution_clock::now();
auto time4 = end_time4 - start_time4;
d = std::chrono::duration_cast<std::chrono::microseconds>(time4).count();
std::cout << "f4 took "
<< (d / 100)
<< " microseconds.\n";
//**********
auto start_time5 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f5 (1000, a, (sizeof(a)/sizeof(a[0])));
}
auto end_time5 = std::chrono::high_resolution_clock::now();
auto time5 = end_time5 - start_time5;
d = std::chrono::duration_cast<std::chrono::microseconds>(time5).count();
std::cout << "f5 took "
<< (d / 100)
<< " microseconds reverse.\n";
//**********
std::cout << "\n";
//**********
double d;
//auto v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "vector\n";
//**********
auto start_time = std::chrono::high_resolution_clock::now();
//for (int t = 0; t < 100; t++)
for (auto t = 0; t != 100; ++t)
{
for (int s = 0; s < 1000; s++)
f0 (1000, v);
}
auto end_time = std::chrono::high_resolution_clock::now();
auto time = end_time - start_time;
d = std::chrono::duration_cast<std::chrono::microseconds>(time).count();
std::cout << "f0 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time6 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
//for (auto t = 0; t != 100; ++t)
{
for (int s = 0; s < 1000; s++)
f0 (1000, v);
}
auto end_time6 = std::chrono::high_resolution_clock::now();
auto time6 = end_time6 - start_time6;
d = std::chrono::duration_cast<std::chrono::microseconds>(time6).count();
std::cout << "f6 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time1 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f1 (1000, v);
}
auto end_time1 = std::chrono::high_resolution_clock::now();
auto time1 = end_time1 - start_time1;
d = std::chrono::duration_cast<std::chrono::microseconds>(time1).count();
std::cout << "f1 took " << (d / 100) << " microseconds.\n";
//**********
auto start_time2 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f2 (1000, v);
}
auto end_time2 = std::chrono::high_resolution_clock::now();
auto time2 = end_time2 - start_time2;
d = std::chrono::duration_cast<std::chrono::microseconds>(time2).count();
std::cout << "f2 took " << (d / 100) << " microseconds reverse.\n";
//**********
auto start_time3 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f3 (1000, v);
}
auto end_time3 = std::chrono::high_resolution_clock::now();
auto time3 = end_time3 - start_time3;
d = std::chrono::duration_cast<std::chrono::microseconds>(time3).count();
std::cout << "f3 took " << (d / 100)
<< " microseconds.\n";
//**********
int a[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "\narray\n";
//**********
auto start_time4 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f4 (1000, a, (sizeof(a)/sizeof(a[0])));
}
auto end_time4 = std::chrono::high_resolution_clock::now();
auto time4 = end_time4 - start_time4;
d = std::chrono::duration_cast<std::chrono::microseconds>(time4).count();
std::cout << "f4 took "
<< (d / 100)
<< " microseconds.\n";
//**********
auto start_time5 = std::chrono::high_resolution_clock::now();
for (int t = 0; t < 100; t++)
{
for (int s = 0; s < 1000; s++)
f5 (1000, a, (sizeof(a)/sizeof(a[0])));
}
auto end_time5 = std::chrono::high_resolution_clock::now();
auto time5 = end_time5 - start_time5;
d = std::chrono::duration_cast<std::chrono::microseconds>(time5).count();
std::cout << "f5 took "
<< (d / 100)
<< " microseconds reverse.\n";
//**********
std::cout << "\n";
To copy to clipboard, switch view to plain text mode
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f0(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (const int& i : v) // access by const reference
{
k = i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f1(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (auto i : v) // access by value, the type of i is int
{
k = i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f2(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (auto i = v.end() - 1; i >= v.begin(); --i) //reverse
{
k = *i;
//qDebug() << k << *i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f3(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (int i = 0; i <= v.size(); i++) //conventional style
{
//k = v.at(i); //ok both of these are similar times
k = v[i]; //ok
//qDebug() << k << i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f4(int n, int arr[], int size) {
int k;
for (int i = 0; i < n; i++)
{
for (int i = 0; i <= size - 1; i++)
{
k = arr[i]; //ok
//qDebug() << k << i << j;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f5(int n, int arr[], int size) {
int k;
for (auto i = 0; i != n; ++i)
{
for (auto i = size - 1; i >= 0; --i) //reverse
{
k = arr[i]; //ok
//qDebug() << arr[i];
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f0(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (const int& i : v) // access by const reference
{
k = i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f1(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (auto i : v) // access by value, the type of i is int
{
k = i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f2(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (auto i = v.end() - 1; i >= v.begin(); --i) //reverse
{
k = *i;
//qDebug() << k << *i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f3(int n, std::vector<int> v) {
int k;
for (int i = 0; i < n; i++)
{
for (int i = 0; i <= v.size(); i++) //conventional style
{
//k = v.at(i); //ok both of these are similar times
k = v[i]; //ok
//qDebug() << k << i;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f4(int n, int arr[], int size) {
int k;
for (int i = 0; i < n; i++)
{
for (int i = 0; i <= size - 1; i++)
{
k = arr[i]; //ok
//qDebug() << k << i << j;
}
}
}
#pragma GCC pop_options
#pragma GCC push_options
#pragma GCC optimize ("O0")
void MainWindow::f5(int n, int arr[], int size) {
int k;
for (auto i = 0; i != n; ++i)
{
for (auto i = size - 1; i >= 0; --i) //reverse
{
k = arr[i]; //ok
//qDebug() << arr[i];
}
}
}
#pragma GCC pop_options
To copy to clipboard, switch view to plain text mode
Bookmarks