By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Question about std:string & std:cout

P: n/a
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?

Apr 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"zhege" writes:

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.


Apr 10 '06 #2

P: n/a
zhege <pe*****@gmail.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.