473,414 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Efficient reading of floating point arrays

I'm new to C#, so please forgive me if this is elementary. I have a file
that consists of, say, one thousand (4-byte) floating points, and I'd like to
read it into an array of floats as efficiently as possible. In C/C++ I'd
simply malloc an array of bytes, read them into my allocated buffer, and cast
the pointer to the buffer as a pointer to a float. In C#, I can read in the
array of bytes like so:

FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] buffer = r.ReadBytes(4000);

Now how would I convert this to an array of 1000 floating points without
converting 4 bytes at a time? Or, is there a way to read 1000 floats
directly from the file without going one at a time? Currently, I'm doing
this:

FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
float[] buffer = new float[1000];

for (int i=0;i<1000;i++)
{
buffer[i] = r.ReadSingle();
}

This seems awfully inefficient. Is there a better way?

Thank you,

Keith Kingsley

--
What if there were no hypothetical situations?
Nov 16 '05 #1
1 6619
Keith Kingsley <Ke***********@discussions.microsoft.com> wrote:
I'm new to C#, so please forgive me if this is elementary. I have a file
that consists of, say, one thousand (4-byte) floating points, and I'd like to
read it into an array of floats as efficiently as possible. In C/C++ I'd
simply malloc an array of bytes, read them into my allocated buffer, and cast
the pointer to the buffer as a pointer to a float. In C#, I can read in the
array of bytes like so:

FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] buffer = r.ReadBytes(4000);
Not that you need to use BinaryReader at all, of course - you can just
use Stream.Read (taking care to note the number of bytes actually
read).
Now how would I convert this to an array of 1000 floating points without
converting 4 bytes at a time? Or, is there a way to read 1000 floats
directly from the file without going one at a time? Currently, I'm doing
this:

FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
float[] buffer = new float[1000];

for (int i=0;i<1000;i++)
{
buffer[i] = r.ReadSingle();
}

This seems awfully inefficient. Is there a better way?


That looks like the best way to me, to be honest. If you're worried
about efficiency, have you actually measured the current performance
and found it to be inadequate?

Buffer.BlockCopy may well work for you, but I don't think it's as clean
a way as the above, personally.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
0
by: Matthias Drescher | last post by:
Hi there, I have a litte problem while reading from a Excelfile. Im try to read a 4 byte value and convert it into an integer or floating point value with the following criteria: Thats what...
3
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID,...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
74
by: copx | last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that properly written C++ outperforms C code. I will just copy his first example here, which is supposed to demonstrate how C++...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
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...

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.