Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #10372 > unrolled thread
| Started by | sara <sarasara82@gmail.com> |
|---|---|
| First post | 2011-11-30 11:31 -0800 |
| Last post | 2011-12-02 19:58 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Writing to file sara <sarasara82@gmail.com> - 2011-11-30 11:31 -0800
Re: Writing to file Lew <lewbloch@gmail.com> - 2011-11-30 14:20 -0800
Re: Writing to file Roedy Green <see_website@mindprod.com.invalid> - 2011-12-02 01:33 -0800
Re: Writing to file Arne Vajhøj <arne@vajhoej.dk> - 2011-12-02 19:58 -0500
| From | sara <sarasara82@gmail.com> |
|---|---|
| Date | 2011-11-30 11:31 -0800 |
| Subject | Writing to file |
| Message-ID | <e1b7d157-4018-44a7-8176-3a2f308562b8@cu3g2000vbb.googlegroups.com> |
Hi All,
I have a program which generates many lines of data where each line
includes an integer and two floats. I want to know the fastest way to
write these generated lines to a file. Currently, I am writing to a
binary file as follows:
FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(fos);
for (n : N) {
dos.writeInt(n.id);
dos.writeFloat(x.floatValue());
dos.writeFloat(y.floatValue());
}
However it seems that this approach is very slow. Can I use any kind
of buffering technique to speed up writing to binary file?
Best
Sara
[toc] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-11-30 14:20 -0800 |
| Message-ID | <16014809.23.1322691639630.JavaMail.geo-discussion-forums@prgi20> |
| In reply to | #10372 |
On Wednesday, November 30, 2011 11:31:33 AM UTC-8, sara wrote:
> Hi All,
>
> I have a program which generates many lines of data where each line
> includes an integer and two floats. I want to know the fastest way to
> write these generated lines to a file. Currently, I am writing to a
> binary file as follows:
> FileOutputStream fos = new FileOutputStream(fileName);
> DataOutputStream dos = new DataOutputStream(fos);
> for (n : N) {
> dos.writeInt(n.id);
> dos.writeFloat(x.floatValue());
> dos.writeFloat(y.floatValue());
> }
>
> However it seems that this approach is very slow. Can I use any kind
> of buffering technique to speed up writing to binary file?
Have you considered reading the API docs?
<http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html>
Note that you will need to build the DataOutputStream on top of the BufferedOutputStream rather than the other way around.
As to whether this helps performance, what measurements have you done, and how do you know that the result is "very slow"? Compared to what?
What were the conditions of the measurements (other load on the system, hard drive configuration, etc.)?
--
Lew
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-12-02 01:33 -0800 |
| Message-ID | <im6hd71hha4eorbj4rmoj7ghjgvt595nni@4ax.com> |
| In reply to | #10372 |
On Wed, 30 Nov 2011 11:31:33 -0800 (PST), sara <sarasara82@gmail.com>
wrote, quoted or indirectly quoted someone who said :
>DataOutputStream dos = new DataOutputStream(fos);
>for (n : N) {
>dos.writeInt(n.id);
>dos.writeFloat(x.floatValue());
See http://mindprod.com/applet/fileio.html
It will generate you code to do this. Make sure you ask for buffered
if you want speed.
Get rid of Float objects and just use pure float primitives if you
want speed.
There is not much overhead, just flipping the little endian Intel uses
internally to big endian.
--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2011-12-02 19:58 -0500 |
| Message-ID | <4ed9744e$0$294$14726298@news.sunsite.dk> |
| In reply to | #10372 |
On 11/30/2011 2:31 PM, sara wrote:
> I have a program which generates many lines of data where each line
> includes an integer and two floats. I want to know the fastest way to
> write these generated lines to a file. Currently, I am writing to a
> binary file as follows:
> FileOutputStream fos = new FileOutputStream(fileName);
> DataOutputStream dos = new DataOutputStream(fos);
> for (n : N) {
> dos.writeInt(n.id);
> dos.writeFloat(x.floatValue());
> dos.writeFloat(y.floatValue());
> }
>
> However it seems that this approach is very slow. Can I use any kind
> of buffering technique to speed up writing to binary file?
You can use a BufferedOutputStream between the DataOutputStream
and the FileOutputStream.
But I am a bit skeptical about the choice of format for
persisting data.
Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web