Question about std:string & 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?
 
J

Jakob Bieling

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?

As you already implied, you will have to measure this yourself. My
guess is, that both will be identical due to optimizations done by the
compiler.

To measure the execution time, you can either use timing facilities
of your OS, or you can stuff your code in a loop, have it execute a
couple of times (be aware that your looping code might be optimized away
if you are not careful) and use the 'time' function to determine the
elapsed number of seconds. Your OS might provide timers with a higher
resolution, though.

hth
 
O

osmium

:

Is there any
method for measuring the efficiency of programs?


When you can measure what you are speaking about, and express it in numbers,
you know something about it; but when you cannot measure it, when you cannot
express it in numbers, your knowledge of it is of a meager and
unsatisfactory kind; it may be the beginning of knowledge, but you have
scarcely, in your thoughts, advanced it to the stage of science.

-- Lord Kelvin

There is not even a *definition* of efficiency of programs, so clearly you
can't measure it.
 

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,780
Messages
2,569,614
Members
45,289
Latest member
stfp04

Latest Threads

Top