Hello,
Playing with the 'auto' keyword.
I am trying to do a test on speed with vectors against arrays.
As you can see using a vector works fine, using an array does'nt.
Arrays always gives a time of zero.
Can some one please point out my error?
Thanks
auto v = {10, 1, 2, 3, 4, 5};
auto start_time3 = std::chrono::high_resolution_clock::now();
for (int s = 0; s < 250000; s++)
f3 (2147483647, v);
auto end_time3 = std::chrono::high_resolution_clock::now();
auto time3 = end_time3 - start_time3;
std::cout << "f3 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time3).count()
//f3 took 46879000 to run.
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++)
{
//k = v.at(i); //ok both of these are similar times
k = v[i]; //ok
//qDebug() << k << i;
}
}
}
auto v = {10, 1, 2, 3, 4, 5};
auto start_time3 = std::chrono::high_resolution_clock::now();
for (int s = 0; s < 250000; s++)
f3 (2147483647, v);
auto end_time3 = std::chrono::high_resolution_clock::now();
auto time3 = end_time3 - start_time3;
std::cout << "f3 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time3).count()
//f3 took 46879000 to run.
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++)
{
//k = v.at(i); //ok both of these are similar times
k = v[i]; //ok
//qDebug() << k << i;
}
}
}
To copy to clipboard, switch view to plain text mode
int a[] = {10, 1, 2, 3, 4, 5};
auto start_time4 = std::chrono::high_resolution_clock::now();
for (int s = 0; s < 250000; s++)
f4 (2147483647, a, (sizeof(a)/sizeof(a[0])));
auto end_time4 = std::chrono::high_resolution_clock::now();
auto time4 = end_time4 - start_time4;
std::cout << "f4 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time4).count()
//f4 took 0 to run.
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;
}
}
}
int a[] = {10, 1, 2, 3, 4, 5};
auto start_time4 = std::chrono::high_resolution_clock::now();
for (int s = 0; s < 250000; s++)
f4 (2147483647, a, (sizeof(a)/sizeof(a[0])));
auto end_time4 = std::chrono::high_resolution_clock::now();
auto time4 = end_time4 - start_time4;
std::cout << "f4 took " << std::chrono::duration_cast<std::chrono::nanoseconds>(time4).count()
//f4 took 0 to run.
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;
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks