473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(400 0);

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 6712
Keith Kingsley <Ke***********@ discussions.mic rosoft.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(400 0);
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.BlockCop y may well work for you, but I don't think it's as clean
a way as the above, personally.

--
Jon Skeet - <sk***@pobox.co m>
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
3774
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 databases, arrays, etc. but have not worked with binary data files. http://www.performancecentral.net/C...3Loop/3Loop.php click on "Download Performance".
0
1624
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 the definiton says: An RK valie is an encoded integer or floating-point value. RK values have a size of 4 bytes and are used to decrease the size for floating-point values.
3
9618
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, next 4 byte (int) = serial number etc. The first problem is Big Endian/ Little Endian problem. I can decipher if the format is big or little endian. But got confuse as to how to decipher the data.
8
9528
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 contains the following three floating-point numbers: 1.0 2.0 3.0
24
2241
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 floating point when dealing with dollars and cents.
7
3392
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 discuss about ways to emulate floating point calculations with just long int or long long int. For eg., if i've a formula X=(1-b)*Y + b*Z in floating point domain, i can calculate X with just long ints (but, some data may be lost in final division;...
13
3338
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
13
4148
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 sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
74
4834
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++ abstractions do not only make code easier to understand but also make it more efficient: === The simplest specific example I can think of is a program to find the mean and median of a sequence of double precision floating-point numbers read...
0
8888
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
9257
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
9176
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,...
1
6702
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
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.