Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10326

Re: Best way to write the vector content to a file.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Best way to write the vector content to a file.
Date Tue, 20 Sep 2011 19:45:21 +1200
Lines 39
Message-ID <9dqukhF76kU10@mid.individual.net> (permalink)
References <31086262.1123.1316487944574.JavaMail.geo-discussion-forums@yqaa12> <75b9f87a-e967-4d67-982c-e2bd020ccb72@s16g2000yqc.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net fuG6MtuZTMRhbLPCw/iw/gS0nrj6QVtLfr76GljXHmkydTW3wN
Cancel-Lock sha1:tCrhh7vUHLHoyD2h2YdhARGSG4c=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <75b9f87a-e967-4d67-982c-e2bd020ccb72@s16g2000yqc.googlegroups.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:10326

Show key headers only | View raw


On 09/20/11 07:37 PM, Goran wrote:
> On Sep 20, 5:05 am, sravanreddy001<sravanreddy...@gmail.com>  wrote:
>> Hi,
>>
>> Can anyone give the best way (faster) to write the vector contents to a file?
>>
>> Is there a better way to do it than below method?
>>
>>          for(unsigned i=0;i<terms.size();i++){
>>                  sprintf(temp,"%d %s\n",i,terms[i].c_str());
>>                  ids_n_terms = ids_n_terms + temp;
>>          }
>>
>> Is the string addition a very expensive operation? (will a new memory allocation takes place)
>
> Well, yes. The above loop is bound to get very expensive, because you
> want to append. That means, from time to time (possibly at every
> append!), "allocate more space, copy old string there, append new
> content, free old space".
>
> You should instead just use ofstream, and play with it's rdbuf-
>> pubsetbuf to find a sweet spot for speed (depends on the system, but
> reasonable buffer sizes like 1K-8K (1024 to 8192) are a good guess). I
> wouldn't even be surprised if you find that the default works just as
> well. E.g.
>
> ofstream f(whatever);
> char buffer[try sizes here]; // speed to be gained here.
> f.rdbuf->pubsetbuf(buffer, sizeof(buffer));
>
> your loop here, but instead of sprintf and +, do
>
>     f<<  i<<  " "<<  terms[i]<<  endl;

Flushing after each line rather defeats the effort of fiddling with the 
ofstream's buffer!

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-19 20:05 -0700
  Re: Best way to write the vector content to a file. Ian Collins <ian-news@hotmail.com> - 2011-09-20 15:12 +1200
  Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-19 20:28 -0700
    Re: Best way to write the vector content to a file. Ian Collins <ian-news@hotmail.com> - 2011-09-20 15:37 +1200
      Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-19 20:50 -0700
        Re: Best way to write the vector content to a file. Ian Collins <ian-news@hotmail.com> - 2011-09-20 16:03 +1200
      Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-19 20:47 -0700
        Re: Best way to write the vector content to a file. Ian Collins <ian-news@hotmail.com> - 2011-09-20 16:09 +1200
    Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-19 20:29 -0700
  Re: Best way to write the vector content to a file. Juha Nieminen <nospam@thanks.invalid> - 2011-09-20 06:44 +0000
  Re: Best way to write the vector content to a file. Goran <goran.pusic@gmail.com> - 2011-09-20 00:37 -0700
    Re: Best way to write the vector content to a file. Ian Collins <ian-news@hotmail.com> - 2011-09-20 19:45 +1200
      Re: Best way to write the vector content to a file. Goran <goran.pusic@gmail.com> - 2011-09-20 02:01 -0700
  Re: Best way to write the vector content to a file. Rui Maciel <rui.maciel@gmail.com> - 2011-09-20 10:56 +0100
  Re: Best way to write the vector content to a file. Fulvio Esposito <esposito.fulvio@gmail.com> - 2011-09-20 04:03 -0700
  Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-20 05:00 -0700
    Re: Best way to write the vector content to a file. Goran <goran.pusic@gmail.com> - 2011-09-20 06:55 -0700
    Re: Best way to write the vector content to a file. Paavo Helde <myfirstname@osa.pri.ee> - 2011-09-20 13:39 -0500
  Re: Best way to write the vector content to a file. sravanreddy001 <sravanreddy001@gmail.com> - 2011-09-20 17:50 -0700

csiph-web