473,404 Members | 2,213 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,404 software developers and data experts.

How to write array of doubles to stream without using a loop?

Hello,

I have been trying to figure out how to write an array of doubles (in
this specific case) to a binary stream without using a loop. What I
have been doing is...

foreach(double d in TraceData)
{
instanceOfBinaryWriter.Write(d);
}

The loop is introducing overhead I don't want. In unmanaged c++, I
would just do something like...

fwrite(buffer, sizeof(double), numArrayElements, fileStream)

Is their an equivalent using c#?

Thanks,
Nov 16 '05 #1
5 5955
Surely fwrite would have a loop inside it? If it does, then surely it
doesn't matter if the loop occurs in your code or in library code?

"John Dumais" <no****************@agilent.com> wrote in message
news:11**************@cswreg.cos.agilent.com...
Hello,

I have been trying to figure out how to write an array of doubles (in this
specific case) to a binary stream without using a loop. What I have been
doing is...

foreach(double d in TraceData)
{
instanceOfBinaryWriter.Write(d);
}

The loop is introducing overhead I don't want. In unmanaged c++, I would
just do something like...

fwrite(buffer, sizeof(double), numArrayElements, fileStream)

Is their an equivalent using c#?

Thanks,

Nov 16 '05 #2
John,

You won't really be able to do that. It would be easier if you had
direct memory access, but unfortunately, you don't. The only thing that you
could do is do a conversion to a byte array, but you know that will
introduce overhead as well (that routine has to loop through as well to do
the conversion).

The only other option you would have is if your method can use unsafe
code. That way, you could cast the double array to a double pointer, and
then cast that to a pointer of a byte, and then pass the byte to the method
without having to loop through to get a byte array.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dumais" <no****************@agilent.com> wrote in message
news:11**************@cswreg.cos.agilent.com...
Hello,

I have been trying to figure out how to write an array of doubles (in this
specific case) to a binary stream without using a loop. What I have been
doing is...

foreach(double d in TraceData)
{
instanceOfBinaryWriter.Write(d);
}

The loop is introducing overhead I don't want. In unmanaged c++, I would
just do something like...

fwrite(buffer, sizeof(double), numArrayElements, fileStream)

Is their an equivalent using c#?

Thanks,

Nov 16 '05 #3
> You won't really be able to do that. It would be easier if you had
direct memory access, but unfortunately, you don't. The only thing that you could do is do a conversion to a byte array, but you know that will
introduce overhead as well (that routine has to loop through as well to do
the conversion).
Take a look at Buffer.BlockCopy. It only works with primitive types. But I
remember looking at the rotor source code and its an internal call and does
a memcopy , memove, mem something but not a for loop. The docs say "This
class provides better performance for manipulating primitive types than
similar methods in the System.Array class.". Here is a sample that did work.

Dim Ds() As Integer = {1, 2, 3}
Dim I() As Byte = Array.CreateInstance(GetType(Byte), Buffer.ByteLength(Ds))
Buffer.BlockCopy(Ds, 0, I, 0, Buffer.ByteLength(Ds))

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ew****************@TK2MSFTNGP14.phx.gbl... John,

You won't really be able to do that. It would be easier if you had
direct memory access, but unfortunately, you don't. The only thing that you could do is do a conversion to a byte array, but you know that will
introduce overhead as well (that routine has to loop through as well to do
the conversion).

The only other option you would have is if your method can use unsafe
code. That way, you could cast the double array to a double pointer, and
then cast that to a pointer of a byte, and then pass the byte to the method without having to loop through to get a byte array.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Dumais" <no****************@agilent.com> wrote in message
news:11**************@cswreg.cos.agilent.com...
Hello,

I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is...

foreach(double d in TraceData)
{
instanceOfBinaryWriter.Write(d);
}

The loop is introducing overhead I don't want. In unmanaged c++, I would just do something like...

fwrite(buffer, sizeof(double), numArrayElements, fileStream)

Is their an equivalent using c#?

Thanks,


Nov 16 '05 #4
John Dumais <no****************@agilent.com> wrote:
I have been trying to figure out how to write an array of doubles (in
this specific case) to a binary stream without using a loop. What I
have been doing is...

foreach(double d in TraceData)
{
instanceOfBinaryWriter.Write(d);
}

The loop is introducing overhead I don't want.


Have you measured the overhead? Is it definitely causing a problem?
There's a tendency to assume that things like this are bottlenecks
without experimentation - that may not be the case here, but it's worth
knowing before we get into much riskier code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] wrote:
Have you measured the overhead? Is it definitely causing a problem?
There's a tendency to assume that things like this are bottlenecks
without experimentation - that may not be the case here, but it's worth
knowing before we get into much riskier code.

Not specifically in C#, because I can't get an apples-to-apples
comparison, but my experience has been that most run-time
libraries work better when you supply them with one hunk of data
as opposed to supplying many little hunks in a loop. The attached
example will illustrate. It's in C++, because I can't come up
with a reasonable comarison in C#. Sorry in advance -- I wrote
this illustration on my Linux machine. I don't have a Windows
PC at home. I ran the program 10 times in a loop and got the
following results.
Using loop
Elapsed time: 2 seconds, 223613 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 725197 micro-seconds
Using loop
Elapsed time: 3 seconds, 256561 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 277398 micro-seconds
Using loop
Elapsed time: 2 seconds, 224807 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 45266 micro-seconds
Using loop
Elapsed time: 2 seconds, 215541 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 231558 micro-seconds
Using loop
Elapsed time: 2 seconds, 221532 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 278577 micro-seconds
Using loop
Elapsed time: 2 seconds, 220584 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 11936 micro-seconds
Using loop
Elapsed time: 2 seconds, 215263 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 28349 micro-seconds
Using loop
Elapsed time: 2 seconds, 223527 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 273473 micro-seconds
Using loop
Elapsed time: 2 seconds, 226312 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 7940 micro-seconds
Using loop
Elapsed time: 2 seconds, 217676 micro-seconds
Writing in a single hunk
Elapsed time: 1 seconds, 23728 micro-seconds

The program...
#include <cstdio>
#include <cstdlib>
#include <sys/time.h>

typedef void(*WriterFuncPtr)(FILE*, double*, size_t);

void timeIt(WriterFuncPtr funcPtr, FILE *fp, double *data,
size_t numDataPoints)
{
timeval startTime = {0, 0};
timeval endTime = {0, 0};
timeval elapsedTime = {0, 0};

if( ! gettimeofday(&startTime, 0)){
funcPtr(fp, data, numDataPoints);

if( ! gettimeofday(&endTime, 0)){
if(startTime.tv_usec > endTime.tv_usec){
endTime.tv_usec += 1000000;
endTime.tv_sec--;
}

elapsedTime.tv_usec = endTime.tv_usec - startTime.tv_usec;
elapsedTime.tv_sec = endTime.tv_sec - startTime.tv_sec;

printf("Elapsed time: %ld seconds, %ld micro-seconds\n",
elapsedTime.tv_sec, elapsedTime.tv_usec);
}
}

}

void checkFileContents(FILE *fp)
{
double data = 0;

size_t numRead = 0;

do{
numRead = fread(&data, sizeof(data), 1, fp);
printf("%lf\n", data);
} while(numRead > 0);
}

void writeDataUsingLoop(FILE *fp, double *data, size_t numDataPoints)
{
for(size_t i = 0; i < numDataPoints; ++i){
(void)fwrite(data++, sizeof(double), 1, fp);
}
}

void writeDataInOneHunk(FILE *fp, double *data, size_t numDataPoints)
{
(void)fwrite(data, sizeof(double), numDataPoints, fp);
}

int main(void)
{
FILE *fp = fopen("data", "w");
if(fp){
const size_t numDataPoints = 8 * 1024 * 1024;
double *data = (double*)malloc(numDataPoints * sizeof(double));
if(data){
for(size_t i = 0; i < numDataPoints; ++i){
data[i] = i;
}

printf("Using loop\n");
timeIt(writeDataUsingLoop, fp, data, numDataPoints);

fclose(fp);

fp = fopen("data", "w");
if(fp){
printf("Writing in a single hunk\n");
timeIt(writeDataInOneHunk, fp, data, numDataPoints);

fclose(fp);
fp = 0;
}

free(data);
}
}

/*
fp = fopen("data", "r");
if(fp){
checkFileContents(fp);

fclose(fp);
fp = 0;
}
*/

return 0;
}


Thanks,
Nov 16 '05 #6

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

Similar topics

50
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
1
by: RP2001 | last post by:
Other than using the BinaryReader Class, is there any other way to read in from a binary stream/file? Thanks! -RP
1
by: Chenzo | last post by:
I have an issue where the provided .wsdl file is being generated incorrectly with the WSDL.exe tool. I have to use digital client certificates for authentication and it has been determined...by...
1
by: Rejimonb | last post by:
Can I fill a combobox without using a loop. Using a loop will be time consuming. Reji
0
by: saroravi | last post by:
How can we write a cobol program without using session table. how Front end (PB) retrieve the data
1
by: bhavanirayala | last post by:
Hi, How can I get the hash values without loop? i.e I want to assign the value of a first key to one variable based on the condition.If that condition falis i need to assign the second value...
1
by: mrbones | last post by:
hey basically this script finds stuff on a list which has a bit added on, on the html profile for the name. The names are in a list and I want the names that it finds that have the extra bit wrote...
5
by: dave816 | last post by:
Sorry for the Excel question in an Access forum...................I don't see an Excel forum and there's probably a reason for that but figured I'd give this a shot anyway. Again sorry, delete if...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
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
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...
0
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,...
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...

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.