Question about std:string and std:cout

Z

zhege

I am a beginner of C++;
I have a question about the std:string and std:cout class;

Two pieces of code:
--------------------------------
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
cout<<s1+s2;
}

---------------------------------

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
string s3 = s1 + s2;
cout<< s3;
}

Which one would be run faster or they'r totally the same.Is there any
method for measuring the efficiency of programs?
 
P

Pete C

zhege said:
I am a beginner of C++;
I have a question about the std:string and std:cout class;

Two pieces of code:
--------------------------------
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
cout<<s1+s2;
}

---------------------------------

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1 = "Hii";
string s2 = "MSDN Fans!\n";
string s3 = s1 + s2;
cout<< s3;
}

Which one would be run faster or they'r totally the same.Is there any
method for measuring the efficiency of programs?

Unless you have hard evidence that one is faster than the other, *and*
that this speed is critical to your program's performance, use the form
that is most expressive to you.

That was the most important thing. That said, in this instance the two
versions will probably be identical. In your first piece of code, the
compiler will construct an unnamed temporary object anyway.

For measuring performance, use a profiler. I use cachegrind on linux,
Rational Quantify on Windows or Intel's VTune on either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
BettinaPol

Latest Threads

Top