473,606 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)
{
instanceOfBinar yWriter.Write(d );
}

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

fwrite(buffer, sizeof(double), numArrayElement s, fileStream)

Is their an equivalent using c#?

Thanks,
Nov 16 '05 #1
5 5970
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.co m> wrote in message
news:11******** ******@cswreg.c os.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)
{
instanceOfBinar yWriter.Write(d );
}

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

fwrite(buffer, sizeof(double), numArrayElement s, 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.co m

"John Dumais" <no************ ****@agilent.co m> wrote in message
news:11******** ******@cswreg.c os.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)
{
instanceOfBinar yWriter.Write(d );
}

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

fwrite(buffer, sizeof(double), numArrayElement s, 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.BlockCop y. 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.CreateIns tance(GetType(B yte), Buffer.ByteLeng th(Ds))
Buffer.BlockCop y(Ds, 0, I, 0, Buffer.ByteLeng th(Ds))

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:ew******** ********@TK2MSF TNGP14.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.co m

"John Dumais" <no************ ****@agilent.co m> wrote in message
news:11******** ******@cswreg.c os.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)
{
instanceOfBinar yWriter.Write(d );
}

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

fwrite(buffer, sizeof(double), numArrayElement s, fileStream)

Is their an equivalent using c#?

Thanks,


Nov 16 '05 #4
John Dumais <no************ ****@agilent.co m> 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)
{
instanceOfBinar yWriter.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.co m>
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(*WriterFun cPtr)(FILE*, double*, size_t);

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

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

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

elapsedTime.tv_ usec = endTime.tv_usec - startTime.tv_us ec;
elapsedTime.tv_ sec = endTime.tv_sec - startTime.tv_se c;

printf("Elapsed time: %ld seconds, %ld micro-seconds\n",
elapsedTime.tv_ sec, elapsedTime.tv_ usec);
}
}

}

void checkFileConten ts(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 writeDataUsingL oop(FILE *fp, double *data, size_t numDataPoints)
{
for(size_t i = 0; i < numDataPoints; ++i){
(void)fwrite(da ta++, sizeof(double), 1, fp);
}
}

void writeDataInOneH unk(FILE *fp, double *data, size_t numDataPoints)
{
(void)fwrite(da ta, 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(writeDat aUsingLoop, fp, data, numDataPoints);

fclose(fp);

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

fclose(fp);
fp = 0;
}

free(data);
}
}

/*
fp = fopen("data", "r");
if(fp){
checkFileConten ts(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
17311
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
7533
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
11462
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 another business...that the proxy class was generated by the wsdl.exe tool incorrectly for use with the client certificates. The solution was stated to be to 'manually write a proxy class without using the wsdl.exe tool'. I'm not sure how to do...
1
1734
by: Rejimonb | last post by:
Can I fill a combobox without using a loop. Using a loop will be time consuming. Reji
0
1248
by: saroravi | last post by:
How can we write a cobol program without using session table. how Front end (PB) retrieve the data
1
2276
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 to that variable. The code should be as follows: Here I am getting values by parsing the xml file.
1
1402
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 into a text file. At the moment the code gives the error IOError: Error. myfile = open('name.txt') name = checker myfile.write(name + "/n")
5
5633
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 necessary. I'm adding an apostrophe into the cell value using a loop, but with 40k rows of data it takes 5 minutes. ' For iCol = 1 To 8 ' For lCtr = 2 To lLastRow ' Cells(lCtr, iCol).Select ' sValue =...
0
7981
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8462
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...
0
6803
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
5994
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
5470
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
4011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2458
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
0
1315
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.