473,847 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread 1 byte x N vs N bytes x 1

When reading a binary input stream with fread() one can
read N bytes in two ways :

count=fread(buf fer,1,N,fin); /* N bytes at a time */

or

count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?

Speed matters here, but the form which (I suspect) is faster can't
handle a partial input block. If the input file isn't a multiple
of N it will give an error on the final read, and fread()
provides no possible way of figuring how many, if any of the
data bytes in buffer are valid.

For the program I'm working on at the moment when it's
reading from a tape drive I'm pretty much guaranteed that
the input is a multiple of the block size. If it isn't, then there
was some sort of read error or maybe the wrong block size was
specified. However when it's reading across the network the
input might be some odd size. If it is, it needs to be padded
out to the full block size before it is written to the tape drive.

As far as I can tell the only way to do that is with the first form.
Well, unless I put "dd" with padding turned on in a pipe upstream
from the program, but I'd rather handle this situation internally
within the one program.

Thanks,

David Mathog
Oct 19 '06 #1
5 6358
David Mathog said:
When reading a binary input stream with fread() one can
read N bytes in two ways :

count=fread(buf fer,1,N,fin); /* N bytes at a time */

or

count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?
I doubt it very much, because it's all buffered up anyway. Whichever way you
do it, the implementation is very likely to multiply the two middle
parameters, and do something like this:

bytestoread = size * nobj;
bytesread = _platform_speci fic_stream_read er(buffer, bytestoread, fin);
return bytesread / size;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 19 '06 #2
David Mathog <ma****@caltech .eduwrites:
When reading a binary input stream with fread() one can
read N bytes in two ways :

count=fread(buf fer,1,N,fin); /* N bytes at a time */

or

count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?
No. Typically fread will multiply the second and third arguments
together and read (at least) that many bytes. The two forms are
then equivalent.
--
"...deficie nt support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Oct 19 '06 #3
Ben Pfaff wrote:
David Mathog <ma****@caltech .eduwrites:
When reading a binary input stream with fread() one can
read N bytes in two ways :
count=fread(buf fer,1,N,fin); /* N bytes at a time */
or
count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?

No. Typically fread will multiply the second and third arguments
together and read (at least) that many bytes. The two forms are
then equivalent.
There's still a significant difference in the return value.
Particularly on
end of file. There's also a subtle difference in the contents of the
buffer
on partial read.

But the reason that the parameters are typically multiplied is because
of the standard...

The fread function reads, into the array pointed to by ptr, up to
nmemb
elements whose size is specified by size, from the stream pointed to
by stream. For each object, size calls are made to the fgetc function
and the results stored, in the order read, in an array of unsigned
char
exactly overlaying the object. ... If a partial element is read, its
value
is indeterminate.

So, fread must work even if the buffer is not suitably aligned for an
object of the given size. Beacuse of that, plus the fact that I/O to
disc
itself is typically _much_ slower than any post I/O processing, and
standard I/O functions are synchronous, most implementations won't
bother looking for very minor performance improvement transferring
object size (2nd parameter) chunks.

Aside: Note that fread is typically used on binary streams.
Unfortunately, the standard still allows for binary streams to have
trailing null bytes. That means that using fread to read an unknown
number of objects can fail, because fread can return a count that
isn't actually correct.

So, when using fread/fwrite, it can make life slightly easier for file
formats include a true count of the number of objects being read
before the objects themselves are stored. In which case, unless
there is a particular need to detect partially read objects, you may
as well use the 'sizeof *p,N' form.

--
Peter

Oct 19 '06 #4
Peter Nilsson said:
Ben Pfaff wrote:
>David Mathog <ma****@caltech .eduwrites:
When reading a binary input stream with fread() one can
read N bytes in two ways :
count=fread(buf fer,1,N,fin); /* N bytes at a time */
or
count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?

No. Typically fread will multiply the second and third arguments
together and read (at least) that many bytes. The two forms are
then equivalent.

There's still a significant difference in the return value.
Yes, but I don't think Ben intended to claim otherwise. It seems to me that
his comments were directed only towards the performance aspects that the OP
was asking about.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 20 '06 #5
Richard Heathfield <in*****@invali d.invalidwrites :
Peter Nilsson said:
>Ben Pfaff wrote:
>>David Mathog <ma****@caltech .eduwrites:
When reading a binary input stream with fread() one can
read N bytes in two ways :
count=fread(buf fer,1,N,fin); /* N bytes at a time */
or
count=fread(buf fer,N,1,fin); /* 1 buffer at a time */

I would assume the latter form would be faster, or at least
less of a load on the CPU. That's just an assumption though,
is it typically true?

No. Typically fread will multiply the second and third arguments
together and read (at least) that many bytes. The two forms are
then equivalent.

There's still a significant difference in the return value.

Yes, but I don't think Ben intended to claim otherwise. It seems to me that
his comments were directed only towards the performance aspects that the OP
was asking about.
In context, I meant "equivalent in performance". But I should
have said so explicitly, to avoid confusion.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Oct 20 '06 #6

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

Similar topics

2
2489
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen by the PHP gd2 Library. The problem is that im trying to translate the 'fread' structure into a MySQL blob reading, as follows:
8
15363
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
5
2098
by: syntax | last post by:
hi here is my test code include<stdio.h> #include<stdlib.h> int main() {
8
17197
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
6
3912
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the following code in Windows 2k and compiled it with the gcc(version: 3.2.3) contained in MinGW: #include <stdio.h> #include <stdlib.h> #include <string.h>
13
3554
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could discern, fread all the sudden kept returning the same byte over and over as if it were no longer advancing in the file. I used a hex editor to determine the address of the last byte read in the file. CF was the last address, D0 was not ever read...
20
7557
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
1
2630
by: Marilia | last post by:
Good evening, (I am having a problem that is making me feel stupid. As in, I was expecting some problems (as always), but later on!) I have a binary file which needs to be copied and processed. The binary file contains the data log from a PLC. My simple approach was to use fread. But... This is a fragment of the code: FILE *ifp; ifp = fopen("in.iom", "r"); int *buffer;
4
3375
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and i'm experiencing unexpected behavior using fread with a binary file. Look at this code: i have a jpeg image of 2290 bytes long, but fread cannot read it correctly, like fgetc does:
0
9892
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...
1
10718
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
10347
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9490
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...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
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
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3168
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.