Click here to Skip to main content
16,008,719 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: writing contents of a deque to a file Pin
Nemanja Trifunovic12-Mar-02 7:05
Nemanja Trifunovic12-Mar-02 7:05 
GeneralRe: writing contents of a deque to a file Pin
Joaquín M López Muñoz12-Mar-02 8:04
Joaquín M López Muñoz12-Mar-02 8:04 
GeneralRe: writing contents of a deque to a file Pin
Nemanja Trifunovic12-Mar-02 8:43
Nemanja Trifunovic12-Mar-02 8:43 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith12-Mar-02 12:01
Tim Smith12-Mar-02 12:01 
GeneralRe: writing contents of a deque to a file Pin
Joaquín M López Muñoz12-Mar-02 19:50
Joaquín M López Muñoz12-Mar-02 19:50 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith13-Mar-02 2:03
Tim Smith13-Mar-02 2:03 
GeneralRe: writing contents of a deque to a file Pin
Joaquín M López Muñoz13-Mar-02 2:36
Joaquín M López Muñoz13-Mar-02 2:36 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith13-Mar-02 4:13
Tim Smith13-Mar-02 4:13 
Doh, you are right. You were the one who started talking about integers, not him.

But, he never said he wanted to use iostreams either. I use STL, but have never used iostreams because I have yet to see the benefits. I see the problem they are trying to correct with iostreams, but you tend to just trade one set of problems for another set.

As far as speed, iostreams are just a bit faster than fprintfs (1512ms compared to 1402ms). At least that is using the copy version. You can chop another 50ms off the iostream version by using a hand-written loop. However, if you don't use fprintf and just use a temporary scratch buffer formatting with itol, the time it takes to do it using fwrite drops to 781ms. Much faster than iostreams. Now, if you use the same trick with iostreams and pre-format the number into a temp buffer, the iostreams version only takes 591ms. Now, we are getting down to measuring each methods underlying performance. It seems that iostreams might have a better buffering system. Or is it. If you change the code slightly and print each integer on one line using endl and include an fflush in the fwrite version to be fair, the fwrite version now takes 3956ms where the iostreams version takes 17595ms.

So, how do the two methods compare? With the fprintf version, half your time is taken up with that fancy formatting string parsing and deciding how to format the value. In the case of iostreams there still seems to be an excessive amount of overhead just deciding how to format the value. When you compare the two, the extra overhead of parsing the fprintf format string is significant. Next, with the iostream version, it turns out those fancy STL algorithm bite us in the butt when it comes to performance. In this case we took around a 3-4% hit using them. This is consistent with other cases I have seen.

When you take all the fancy formatting out of the picture and just write raw strings to the file, performance for both the fprintf/fwrite version and the iostream version jumps significantly. However, the iostream version is still beating the fprintf/fwrite version. Or is it? It is important to remember that iostreams and fwrite operate slightly differently. If you have a case where you want to make sure the output is displayed on the screen, you have to use some type of flush (i.e. endl) with iostreams. Doing so incurs a huge hit in performance.

So, which one is faster? Neither. Depending on your application, fprintf/fwrite can be a bit slower or significantly faster. But it is also important to remember that in a real world application, the speed of the disk IO will greatly overshadow any differences in these two implementations. That is unless you do a lot of endls in iostreams when you really just need a CR. That flush can be a nasty killer.

Tim Smith

I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
GeneralRe: writing contents of a deque to a file Pin
Joaquín M López Muñoz13-Mar-02 11:59
Joaquín M López Muñoz13-Mar-02 11:59 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith13-Mar-02 12:20
Tim Smith13-Mar-02 12:20 
GeneralRe: writing contents of a deque to a file Pin
Nemanja Trifunovic13-Mar-02 12:31
Nemanja Trifunovic13-Mar-02 12:31 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith13-Mar-02 13:16
Tim Smith13-Mar-02 13:16 
GeneralRe: writing contents of a deque to a file Pin
Tim Smith13-Mar-02 13:19
Tim Smith13-Mar-02 13:19 
QuestionHow can I get the accelerator char of a toolbar button? Pin
AmarB12-Mar-02 2:51
AmarB12-Mar-02 2:51 
AnswerRe: How can I get the accelerator char of a toolbar button? Pin
Joaquín M López Muñoz12-Mar-02 3:00
Joaquín M López Muñoz12-Mar-02 3:00 
GeneralRe: How can I get the accelerator char of a toolbar button? Pin
AmarB12-Mar-02 5:38
AmarB12-Mar-02 5:38 
GeneralRe: How can I get the accelerator char of a toolbar button? Pin
Joaquín M López Muñoz12-Mar-02 6:27
Joaquín M López Muñoz12-Mar-02 6:27 
GeneralRe: How can I get the accelerator char of a toolbar button? Pin
AmarB24-Apr-02 3:39
AmarB24-Apr-02 3:39 
GeneralSharing resources across projects Pin
Michael P Butler12-Mar-02 2:38
Michael P Butler12-Mar-02 2:38 
GeneralRe: Sharing resources across projects Pin
Joaquín M López Muñoz12-Mar-02 2:49
Joaquín M López Muñoz12-Mar-02 2:49 
GeneralRe: Sharing resources across projects Pin
Tomasz Sowinski12-Mar-02 3:33
Tomasz Sowinski12-Mar-02 3:33 
GeneralRe: Sharing resources across projects Pin
Carlos Antollini12-Mar-02 3:46
Carlos Antollini12-Mar-02 3:46 
GeneralRadio buttons not playing fair Pin
-Dy12-Mar-02 1:25
-Dy12-Mar-02 1:25 
GeneralRe: Radio buttons not playing fair Pin
Christian Graus12-Mar-02 1:44
protectorChristian Graus12-Mar-02 1:44 
GeneralRe: Radio buttons not playing fair Pin
Tomasz Sowinski12-Mar-02 1:56
Tomasz Sowinski12-Mar-02 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.