Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Strange result
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 43761 of 47562
Post > Topic >>

Strange result

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > Mar 9, 2008 at 09:40 AM

1. The program below should theoretically not run on my old & clunky
machine, 
since theoretically it allocates 2 to 4 GB.  In reality, according to
Windows 
Task Manager, it allocates only some 20 MB tops.  And runs fine, though
slow...

2. With MSVC, and/or with 10.000 or fewer iterations and Op vector
elements, the 
inefficient reference strings are faster than std::string string, as
expected. 
On my machine, with g++ and 100.000 iterations, the opposite happens, and
the 
machine trashes on allocation and deallocation for the ref strings.  I
guess on 
a modern machine that limit must be higher (yet another factor of 10?),
but I'm 
interested whether (1) this can be reproduced, and (2) whether anyone has
any 
explanation (at a guess something causes a lot of memory to be allocated,
but it 
doesn't show up in Task Manager).

Disclaimer: this is late for me, so thinking box not entirely sharp...


#include    <boost/progress.hpp>
#include    <boost/shared_ptr.hpp>
#include    <iostream>
#include    <ostream>
#include    <vector>
#include    <string>

class RefString
{
private:
     boost::shared_ptr<std::string>  myString;
public:
     RefString( size_t n, char c )
     : myString( new std::string( n, c ) )
     {}
};

template< class String >
struct Op_
{
     String                  s;
     std::vector<String>     v;

     Op_(): s( 200, ' ' ), v( 100, s ) {}
     void operator()() { v.insert( v.begin(), s ); }
};

template< class String >
void doTest()
{
     using namespace std;
     typedef Op_<String> Op;
     vector<Op>  op( 100000 );

     boost::progress_timer   timer;
     for( size_t i = 0;  i < op.size();  ++i )
     {
         op[i]();
     }
}

template< class String >
void doNamedTest()
{
     std::cout << typeid(String).name() << ": ";
     doTest<String>();
}

int main()
{
     for( int i = 1;  i <= 5;  ++i )
     {
         std::cout << "TEST #" << i << ":" << std::endl;
         std::cout << std::endl;

         doNamedTest<RefString>();
         doNamedTest<std::string>();
     }
}


Cheers, & TIA.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 




 5 Posts in Topic:
Strange result
"Alf P. Steinbach&qu  2008-03-09 09:40:37 
Re: Strange result
Paul Brettschneider <p  2008-03-09 10:50:24 
Re: Strange result
"Alf P. Steinbach&qu  2008-03-09 11:35:56 
Re: Strange result
Paul Brettschneider <p  2008-03-09 12:23:58 
Re: Strange result
=?UTF-8?B?RXJpayBXaWtzdHL  2008-03-09 11:06:43 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 7:44:03 CDT 2008.