473,382 Members | 1,745 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,382 software developers and data experts.

What's the best way to accumulate data?

Hi,

I have an application that receives data (floats) via a socket. My job
is to receive the bytes, byte-swap them and convert them to floats,
and make them available to the application.

The data arrives in packets. Each packet has a header telling me how
many floats will follow. So I create an array with this many elements
and then read them in to the array. There are usually 10,000 to 30,000
samples in each packet.

Sometimes the data is split between multiple packets. I need to
assemble all the data from all the packets into a single array. This
is my question. What's the best way to do this?

Here was my thought: I would still create arrays for each packet. I
would put each packet array into an array list (I don't know how many
packets there will be), then when all the packets are received (a flag
in the header tells me this), I would copy all the arrays into one
super large array (it may have 200,000 floats in it).

Does this seem reasonable? Any other ideas?

This is a real-time system and I receive a packet, or set of packets,
every 200 miliseconds.

Thanks,
John
Nov 16 '05 #1
8 3173
Hi John,

I think we can use the MemoryStream and the BinaryWriter to accumulate the
data.
Every time a packet arrives, we just check the header to see if this the
last packet and write payload(that the byte array that contain the data we
need) to the memorystream.

e.g.
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
byte[] pb = new byte[16];
//first packet
for (int i =0; i< 16;i++)
pb[i]=(byte)i;
//pb is what we will get from the packet
//Writer from the 4th byte
bw.Write(pb,4,pb.Length-4);
//second packet
for (int i =0; i< 16;i++)
pb[i]=(byte)(i+16);
bw.Write(pb);
byte[] pbms=ms.ToArray();
for (int i=0;i<pbms.Length;i++)
Console.WriteLine(pbms[i]);
bw.Flush();
bw.Close();
ms.Close();

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #2
Hi John,

Did my suggestion help you that use the memorystream to accumulate the data?
If you still have any concern on this issue, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #3
Peter,

Yes, your reply did help. Thank you very much.

I'm still studying the implementation and how it impacts the
performance of my application. If I have more questions, I'll post.

Thank you!
John

v-******@online.microsoft.com ("Peter Huang") wrote in message news:<fw**************@cpmsftngxa10.phx.gbl>...
Hi John,

Did my suggestion help you that use the memorystream to accumulate the data?
If you still have any concern on this issue, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #4
Peter,

If I put bytes in the MemoryStream are they "boxed" or do they stay as
bytes? What about when I convert to an array?

Thanks,
John

v-******@online.microsoft.com ("Peter Huang") wrote in message news:<kr**************@cpmsftngxa10.phx.gbl>...
Hi John,

I think we can use the MemoryStream and the BinaryWriter to accumulate the
data.
Every time a packet arrives, we just check the header to see if this the
last packet and write payload(that the byte array that contain the data we
need) to the memorystream.

e.g.
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
byte[] pb = new byte[16];
//first packet
for (int i =0; i< 16;i++)
pb[i]=(byte)i;
//pb is what we will get from the packet
//Writer from the 4th byte
bw.Write(pb,4,pb.Length-4);
//second packet
for (int i =0; i< 16;i++)
pb[i]=(byte)(i+16);
bw.Write(pb);
byte[] pbms=ms.ToArray();
for (int i=0;i<pbms.Length;i++)
Console.WriteLine(pbms[i]);
bw.Flush();
bw.Close();
ms.Close();

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #5
Peter,

Is there a way that I can re-use the memory stream for subsequent
sweeps of data?

I think I can set the position to 0, but if the length of the second
sweep of data is less than the first, the memorystream won't contract.

I guess I have to:

ms = new MemoryStream();

before every sweep of data?

Thanks,
John

v-******@online.microsoft.com ("Peter Huang") wrote in message news:<kr**************@cpmsftngxa10.phx.gbl>...
Hi John,

I think we can use the MemoryStream and the BinaryWriter to accumulate the
data.
Every time a packet arrives, we just check the header to see if this the
last packet and write payload(that the byte array that contain the data we
need) to the memorystream.

e.g.
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
byte[] pb = new byte[16];
//first packet
for (int i =0; i< 16;i++)
pb[i]=(byte)i;
//pb is what we will get from the packet
//Writer from the 4th byte
bw.Write(pb,4,pb.Length-4);
//second packet
for (int i =0; i< 16;i++)
pb[i]=(byte)(i+16);
bw.Write(pb);
byte[] pbms=ms.ToArray();
for (int i=0;i<pbms.Length;i++)
Console.WriteLine(pbms[i]);
bw.Flush();
bw.Close();
ms.Close();

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #6
John <jo********@hotmail.com> wrote:
If I put bytes in the MemoryStream are they "boxed" or do they stay as
bytes? What about when I convert to an array?


No, they're not bytes, because they're in an array, and an array (of
type byte[]) doesn't require boxing.

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

When we call the MemoryStream.Write or MemoryStream.ToArray method, since
the parameter is byte array and the require parameter is byte, we will not
do the boxing operation. Since we are using the encapsulated class of .net
to manipulate the byte array, we will not care the concrete implement of
the MemoryStream.

If you do care the optimum space and time effiency of the code, I think you
may have to manipulate the byte array yourself.
e.g.
we can declare a bytearray of proper size and them copy the data from the
packet into the bytearray ourselves and we can manipulate the bytearray
with our will.

Also Memory has another method GetBuffer, we can get the underlying buffer
for the memorystream and manipulate the data in the memorystream.
As for reuse the memorystream, I think we can use the SetLength to gear the
size of the memorystream.

e.g.
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
byte[] pb = new byte[15];
//first packet
for (int i =0; i< 15;i++)
pb[i]=(byte)i;
bw.Write(pb);
byte[] pbms=ms.GetBuffer();
byte temp = pbms[0];
pbms[0]=pbms[1];
pbms[1]=temp;
for (int i=0;i<ms.Length ;i++)
Console.Write(pbms[i]);
Console.WriteLine();
//second packet
ms.Position=0;
ms.SetLength(13);
Console.WriteLine(ms.Length);
pb = new byte[13];
for (int i =0; i< 13;i++)
pb[i]=(byte)i;
bw.Write(pb);
pbms=ms.ToArray();
for (int i=0;i<pbms.Length;i++)
Console.Write(pbms[i]);
Console.WriteLine();
bw.Close();
ms.Close();
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #8
Hi John,

Do you have any concern on my suggestion that use the SetLength to adjust
the memorystream?
If you still have any concern on this issue, please feel free to post here.
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #9

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

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
6
by: Brian K. Michalk | last post by:
I have a perl app that is calculating the standard deviation of a 4000 element 16 bit integer array, that has large dynamic content. I.e, the range spans a significant portion of the 16 bits. I...
5
by: cesco | last post by:
I have a set of pairs defined as follow: set< pair<UserEquipment*, double> > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second...
20
by: pratap | last post by:
Could someone clarify how could one reduce the size of an executable code during compile time. Could one use specific compile time flag with makefile or is it advisable to go for dynamic linking....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.