Question about std:string & std:cout



 DEVELOP > c-Plus-Plus > Question about std:string & std:cout

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "zhege"
Date: 10 Apr 2006 12:19:31 PM
Object: Question about std:string & std:cout
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?
.

User: "osmium"

Title: Re: Question about std:string & std:cout 10 Apr 2006 12:29:50 PM
"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.
.

User: "Jakob Bieling"

Title: Re: Question about std:string & std:cout 10 Apr 2006 12:28:21 PM
zhege <peng.jz@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)
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER