473,763 Members | 6,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fwrite output is not efficient (fast) ?

Hi,

recently some C programmer told me that using fwrite/fopen functions
are not efficient because the output that they do to the file is
actually buffered and gets late in writing. Is that true?

regards,

...ab
Oct 24 '08
25 15572
Abubakar wrote:
well he is saying that using the *native* read and write to do the
same task using file descriptors is much faster than the fwrite etc.
He says using the FILE * that is used in case of the fopen/fwrite
buffers data and is not as fast and predictable as read write are.
fwrite() buffers data only if you don't tell it not to, by calling
setvbuf(). When talking about buffered I/O, he's right about the
predictability, if he's talking about predicting when data gets written
to the file. That depends upon the details of the buffering scheme,
which are in general unknown to the user.

However, what he's saying about speed is true only if he's mainly
concerned with the speed with which the first byte reaches a file. If
he's concerned with the speed with which the last byte reaches the file,
buffering is generally faster, at least with sufficiently large files.
Oct 24 '08 #11
James Kuyper <ja*********@ve rizon.netwrote:
Abubakar wrote:
well he is saying that using the *native* read and write to do the
same task using file descriptors is much faster than the fwrite etc.
He says using the FILE * that is used in case of the fopen/fwrite
buffers data and is not as fast and predictable as read write are.

fwrite() buffers data only if you don't tell it not to, by calling
setvbuf(). When talking about buffered I/O, he's right about the
predictability, if he's talking about predicting when data gets written
to the file.
Not even that. The OS may have its own buffers, and so may the drive
firmware. Bottom line, if you want absolute file security, you have to
nail it down to the hardware level. If you don't need that, 99+% of the
time, ISO C <stdio.hfunctio ns are good enough.

Richard
Oct 24 '08 #12
Abubakar said:
Well he read the link and now he says that the replies by Nate
Eldredge and the guy whose email starts with "s0s" have said what
proves what he says is right
They have both argued the opposite point of view. They may or may not have
proved /their/ case, but they certainly haven't proved /his/.
so he says there is no need to reply to anything.
Of course not. If he wishes to continue in blissful ignorance, that's his
choice! :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 24 '08 #13
On 24 Oct, 08:03, Abubakar <abubak...@gmai l.comwrote:
recently some C programmer told me that using fwrite/fopen functions
are not efficient because the output that they do to the file is
actually buffered and gets late in writing. Is that true?
No. Sometimes using fwrite() is less efficient than using
(the non-standard function) write(), sometimes it is
more efficient. For example:

fwrite( buf, 1, 8192, fp );

is likely to be less efficient than

write( fd, buf, 8192 );

but

for( i = 0; i < 8192; i++ )
fwrite( buf + i, 1, 1, fp );

is likely to be more efficient than

for( i = 0; i < 8192; i++ )
write( fd, buf + i, 1 );

In the loop, fwrite() is likely to be more efficient
**because of** the buffering. In the first example, fwrite()
is likely to be less efficient because there is (usually)
an extra data move that doesn't occur with write().

However, the portability of fwrite() is a significant
feature to be considered, as is its ease of use. Unless
you have a verified, measured need to replace fwrite()
with write(), don't bother.
Oct 24 '08 #14
Abubakar wrote:
>
well he is saying that using the *native* read and write to do the
same task using file descriptors is much faster than the fwrite etc.
He says using the FILE * that is used in case of the fopen/fwrite
buffers data and is not as fast and predictable as read write are.

I have given him the link to this discussion and he is going to be
posting his replies soon hopefully.
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. Your top-posting has lost all continuity from
the thread. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/ (taming google)
<http://members.fortune city.com/nnqweb/ (newusers)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 24 '08 #15

"Abubakar" <ab*******@gmai l.comwrote in message news:
>well he is saying that using the *native* read and write to do the
same task using file descriptors is much faster than the fwrite etc.
He says using the FILE * that is used in case of the fopen/fwrite
buffers data and is not as fast and predictable as read write are.
He's right. fwrite() will call the native write functions, which will call
device drivers and the like. As you go down through the levels closer to the
actual hardware, there is more potential for cutting out unnecessary
operations and making things faster. However only if you know what you are
doing (or maybe if the implementation is very slack).
When you move to a new machine it is likely that the optimisations will
become sub-optimal, or the code might not work at all.

Nowadays it is very seldom worth the extra effort and loss of portability in
not calling fwrite(). However that wasn't always true. General-purpose PCs
used to be slower by a factor of 1000 than modern machines, and then you had
to squeeze every last drop of performance out of the machine to make your
games run fast enough.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Oct 25 '08 #16

"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:aI******** *************** *******@bt.com. ..
>
"Abubakar" <ab*******@gmai l.comwrote in message news:
>>well he is saying that using the *native* read and write to do the
same task using file descriptors is much faster than the fwrite etc.
He says using the FILE * that is used in case of the fopen/fwrite
buffers data and is not as fast and predictable as read write are.
Nowadays it is very seldom worth the extra effort and loss of portability
in not calling fwrite(). However that wasn't always true. General-purpose
PCs used to be slower by a factor of 1000 than modern machines, and then
you had to squeeze every last drop of performance out of the machine to
make your games run fast enough.
But sometimes modern machines are being asked to do 1000 times more work.
Performance can still be an issue.

--
Bartc

Oct 25 '08 #17

"Bartc" <bc@freeuk.comw rote in message
But sometimes modern machines are being asked to do 1000 times more work.
Performance can still be an issue.
But disk IO is less likely to be the bottleneck. My programs take up to two
weeks to run on as many processors as I can lay my hands on, however they
only read and write a few kilobytes of data.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 25 '08 #18
Malcolm McLean wrote, On 25/10/08 16:46:
>
"Bartc" <bc@freeuk.comw rote in message
>But sometimes modern machines are being asked to do 1000 times more
work. Performance can still be an issue.
But disk IO is less likely to be the bottleneck.
Except when it is. You know absolutely nothing about the type of program
this might be used for and some programs are definitely disk bound even
with the fastest disk sub-systems.
My programs take up to
two weeks to run on as many processors as I can lay my hands on, however
they only read and write a few kilobytes of data.
So yours are not IO bound, that says nothing about the situations the OP
is concerned with.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 25 '08 #19
"Malcolm McLean" <re*******@btin ternet.comwrite s:
"Bartc" <bc@freeuk.comw rote in message
>But sometimes modern machines are being asked to do 1000 times more
work. Performance can still be an issue.

But disk IO is less likely to be the bottleneck. My programs take up
to two weeks to run on as many processors as I can lay my hands on,
however they only read and write a few kilobytes of data.
If processors increase in speed at a faster rate than
I/O bandwidth does, which happens to be the case, then
I/O can do nothing apart from become more of a bottle-
neck. If you, like me, run /embarassingly parallel/ code,
then more times nothing is nothing, but we're in a very
fortunate minority.

Phil
--
The fact that a believer is happier than a sceptic is no more to the
point than the fact that a drunken man is happier than a sober one.
The happiness of credulity is a cheap and dangerous quality.
-- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion
Oct 25 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3618
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
3
4530
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
3
569
by: email.ttindustries | last post by:
Hello, I am a C/C++ newbie and I have a simple question concerning fast data writing to binary files. Are there any other faster methods than the standard write() method to write to binary data files? I ask this question because I have to store a big amount of data coming from an PCI A/D card with a sampling frequency of 20 MB/s. Now I have to implement some additional computations and would need to speed up the data transfer. I am...
11
4303
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix function write() into a standard C function rather than to extend the existing standard C function...
7
1814
by: Carlo Milanesi | last post by:
Hello, I just completed writing an online book about developing efficient software using the C++ language. You can find it here: http://en.wikibooks.org/wiki/Optimizing_C%2B%2B It is a wiki, that is everyone can change it or only add critical comments to the pages. Everyone is invited to improve it. But before applying major changes, please read these guidelines: http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Guidelines_for_editors
12
8976
by: arnuld | last post by:
WANTED: Even if I do Ctrl-C in the middle of fgets(), fwrite() should write the previously entered data to a file (except if I hit the file-size limit) PROBLEM: If I do a Ctrl-C in the middle of fgets(). fwrite() does not write the data to the file. #include <stdio.h>
0
1318
by: Malcolm McLean | last post by:
Kind of. When I first wrote adventure games, back in the 1980s, the main thing that limited the game was the amount of text you could store in main RAM. Some authors even experimented with separate data tapes - you couldn't rely on the user having a disk drive. Now of course you couldn't possibly generate enough text to fill a PC memory, even though adventure games now employ teams of writers / designers as well as programmers, musicians,...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.