Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I wanted to show my team why move semantics can save resources. So I wrote some code to get some numbers for the talk. However, during this work I found something interesting which I did not fully understand. Copying a vector of pairs took substantially longer than copying a vector of an equivalent struct.
Any ideas why?
Output:
std::pair<int,int>
===============
- sizeof type: 8
- sizeof container: 16
- copy = 80392 microseconds
- move = 6 microseconds
PAIR
====
- sizeof type: 8
- sizeof container: 16
- copy = 6377 microseconds
- move = 8 microseconds
Code:
#include <iostream>
#include <vector>
#include <chrono>
template <typename T>
void
MoveCopyComp()
{
std::vector<T> list;
T dummy;
std::cout << "- sizeof type: " << sizeof(T) << "\n";
std::cout << "- sizeof container: " << sizeof(list) << "\n";
for (int num = 0; num < 1000000; ++num)
list.push_back(dummy);
auto t0 = std::chrono::steady_clock::now();
std::vector<T> copy = list;
auto t1 = std::chrono::steady_clock::now();
std::vector<T> movement = std::move(list);
auto t2 = std::chrono::steady_clock::now();
std::cout << "- copy = "
<< std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()
<< " microseconds\n";
std::cout << "- move = "
<< std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count()
<< " microseconds\n";
}
struct PAIR
{
int a;
int b;
};
int main(int argc, char **argv)
{
std::cout << "std::pair<int,int>\n===============\n";
MoveCopyComp<std::pair<int,int>>();
std::cout << "\nPAIR\n====\n";
MoveCopyComp<PAIR>();
}
Oh face palm! Mike Vine hit it right on the shnoz.
I was using my debug build settings and not optimised.
Here are my optimised results for the same code:
std::pair<int,int>
===============
- sizeof type: 8
- sizeof container: 12
- copy = 2388 microseconds
- move = 0 microseconds
PAIR
====
- sizeof type: 8
- sizeof container: 12
- copy = 2672 microseconds
- move = 0 microseconds
Thanks everyone for the comments :)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
#include <iostream>
#include <cmath>
//This program will calculate the area of a circle and the volume of the sphere using a given
radius value//
using namespace std;
void multivalues(int & , int & , int &, int&) ;
int main()
{
int value, squared, cubed, fourth;
value = 2;
multivalues(value,squared,cubed,fourth);
cout << "The value of the squared is " << squared << endl;
cout << "The value of the cubed is " << cubed << endl;
cout << "The value of the fourth is " << fourth << endl;
return 0;
}
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
This is for an assignment leading to understanding passing by reference. This program should calculate the area of a circle and the volume of the sphere using a given value. It doesn't compile however.
You have a ; where you don't want one:
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
Look at the end of the first line I included.
I'm trying to gain a better grip as to when should I implement move semantics on my code or just delegate that to the compiler, and while performing some basic profiling/benchmarking I got confused as to why moving a vector is actually taking longer than copying it:
#include <chrono>
#include <vector>
#include <utility>
#include <iostream>
#define REPS 10000000
int main(int argc, char const *argv[]) {
// ----------------------------------------------------
std::chrono::high_resolution_clock clock;
auto start = clock.now();
for (int i = 0; i < REPS; i++) {
std::vector< float >(700, 1.54343);
}
auto end = clock.now();
std::cout << "Instantiating vector [x" << std::to_string(REPS) << "]: " << std::chrono::duration_cast< std::chrono::milliseconds >(end - start).count() << " ms" << std::endl;
// ----------------------------------------------------
std::vector< float > src(700, 1.54343);
start = clock.now();
for (int i = 0; i < REPS; i++) {
std::vector< float > dst = src;
}
end = clock.now();
std::cout << "Copying from rvalue [x" << std::to_string(REPS) << "]: " << std::chrono::duration_cast< std::chrono::milliseconds >(end - start).count() << " ms" << std::endl;
// ----------------------------------------------------
start = clock.now();
for (int i = 0; i < REPS; i++) {
std::vector< float > dst = std::vector< float >(700, 1.54343); // vector< float >(700, 1.54343);
}
end = clock.now();
std::cout << "Moving from a temporary [x" << std::to_string(REPS) << "]: " << std::chrono::duration_cast< std::chrono::milliseconds >(end - start).count() << " ms" << std::endl;
}
I get the follwing output:
Instantiating vector [x10000000]: 1160 ms
Copying from rvalue [x10000000]: 1167 ms
Moving from a temporary [x10000000]: 1491 ms
But shouldn't moving it, at worst, be as efficient as copying it (or in this case, initializing a new one)? Is there anything wrong with how I'm profiling this code?
Weirdly enough, I also noticed that commenting the first loop had a significant impact on the time:
Instantiating vector [x10000000]: 0 ms
Copying from rvalue [x10000000]: 1176 ms
Moving from a temporary [x10000000]: 1049 ms
Which may have an obvious explanation, but seems quite strange to me.
You're not moving correctly! In your snippets the line when you want to move, consists of one creation operation and one assigning operation, that take much longer than a single assignment.
If you want to calculate move operation, you can write something like this:
std::vector< float > dst = std::move(src);
My test result without this modification:
Instantiating vector [x10000000]: 22000 ms
Copying from rvalue [x10000000]: 19912 ms
Moving from a temporary [x10000000]: 22694 ms
My test result with this modification:
Instantiating vector [x10000000]: 23187 ms
Copying from rvalue [x10000000]: 20267 ms
Moving from a temporary [x10000000]: 593 ms
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Consider the following program. Why is move slower than copy? I thought that move semantics sped things up considerably when returning locals as rvalues from functions. Can you tell me why moving a large vector is slower than copying it. The benchmark for returning a shared_ptr is provided as a base case.
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
using namespace std;
vector<int> getStringByCopy()
{
vector<int> v;
for (int x = 0; x< 15000000;x++) v.push_back(x);
return v;
}
vector<int>&& getStringByMove()
{
vector<int> v;
for (int x = 0; x< 15000000;x++) v.push_back(x);
return move(v);
}
shared_ptr<vector<int>> getStringByPointer()
{
shared_ptr<vector<int>> v = make_shared<vector<int>>();
for (int x = 0; x< 15000000;x++) v->push_back(x);
return v;
}
int main(int argc, const char * argv[]) {
// insert code here...
chrono::system_clock::time_point begin = chrono::system_clock::now();
vector<int> v = getStringByCopy();
chrono::system_clock::time_point end = chrono::system_clock::now();
cout << v[0] << "copy took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;;
begin = chrono::system_clock::now();
vector<int>&& m = getStringByMove();
end = chrono::system_clock::now();
cout << m[0] << "move took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;;
begin = chrono::system_clock::now();
shared_ptr<vector<int>> sp = getStringByPointer();
end = chrono::system_clock::now();
cout << (*sp)[0] << "pointer took " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << endl;
return 0;
}
The output is:
0copy took 437043
0move took 462803
0pointer took 394549
Your method that is supposed to return per copy already return per move. If you return anything that were allocated on the stack per value in a method and return it, then you get a automatic move.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to remove elements from a vector and it works fine with the erase() methode, but after removing the element the size of the vector still the same.
std::vector<int> myvector;
myvector.push_back (1);
myvector.push_back (2);
myvector.push_back (3);//here the size is 3
myvector.erase(myvector.begin()+1);//I think normally the size should be 2 after removing the element
is there a function that can do that or should I do it manually, I'm new to c++ I checked the documentation and I didn't found a solution for this.
The size is changed then an element of a vector is removed with using member function erase. If you mean capacity then it will not be changed.
Here is a demonstrative program
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v { 1, 2, 3 };
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
v.erase( v.begin() + 1 );
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The output is
v.size() = 3
1 2 3
v.size() = 2
1 3
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's assume C++ for loop in the form of:
for(int i; i<10;i++)
So the integer needs to be allocated in the beginning and then increased at every step the as well as compared. So wouldn't it be faster to do something like that:
f or(int i; i++<10;)
since the variable doesn't need to be loaded into the storage again? espacially when making it volatile?
This little example code gave the same result for all the cases. Does the for loop get optimized anyway or am I missing something?
#include<iostream>
#include<ctime>
int main() {
time_t start,ende;
volatile int dummy = 1;
const int rep = 1000000000;
// Method 1
start = time(0);
for (int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 1: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 2
start = time(0);
for (int i = 0; i++ < rep; )
dummy = 1;
ende = time(0);
std::cout << "Method 2: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 3
start = time(0);
for (volatile int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 3: " << difftime(ende,start)*1000 << " ms" << std::endl;
}
OS: Linux
Compiler: g++
Optimization: standart (no flag)
Compilers are a lot smarter than you think. They won't produce the kind of spurious load that you think they do.
Optimization depends on compiler and its options.
I'd suggest you to disassemble your code and see what's the result of the optimization.
A simple good disassemble e.g. HT editor or IDA version 5 (it free now). For small piece of code it will be easy enough.