Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:56:41+00:00 2026-05-23T10:56:41+00:00

I have a program where i generate a huge matrix and once it is

  • 0

I have a program where i generate a huge matrix and once it is calculated, i have to reuse it at later times. For that reason, i want to cache it to the local hard disk so that i can read it at later times. I am using it simply by writing data to file and then later reading it.

But is there anything special that i should take into consideration for doing such tasks in java. For example, do i need to serialize it or may be do something special. Is there something i should take care for doing such things where i store important application usage data. Should it be plain ASCII/xml or what?
The data is not sensitive, however the integrity of the data is important.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:56:41+00:00Added an answer on May 23, 2026 at 10:56 am

    If your data is really huge, I’d recommend some binary form – this will make it smaller and faster to read and especially parse (XML or JSON are many times slower than reading/writing binary data). Serialization also brings a lot of overhead, so you might want to check DataInputStream and DataOutputStream. If you know you will be writing only numbers of specific type or you know what sequence the data will be in – these are certainly the fastest ones.

    Do not forget to wrap File Streams with Buffered Streams – they will make your operations order of magnitude faster still.

    Something like (8192 is example buffer size- you can tailor it to your needs):

        final File file = null; // get file somehow
        final DataOutputStream dos = new DataOutputStream(
           new BufferedOutputStream(new FileOutputStream(file), 8192));
        try {
            for (int x: ....) { //loop through your matrix (might be different if matrix is sparse)
               for (int y: ....) {
                   if (matrix[x,y] != 0.0) {
                       dos.writeInt(x);
                       dos.writeInt(y);
                       dos.writeDouble(matrix[x,y]);                                     
                   } 
               }
            }
         } finally {
           dos.writeInt(-1); // mark end (might be done differently)
           dos.close();
         }
    

    and input:

        final File file = null; // get file somehow
        final DataInputStream dis = new DataInputStream(
          new BufferedInputStream(new FileInputStream(file), 8192));
        try {
            int x;
            while((x = dis.readInt()) != -1) { 
               int y = dis.readInt();
               double value = dis.readDouble();
               // store x,y, value in matrix
            } 
        } finally {
           dis.close();
        }
    

    as correctly pointed out by Ryan Amos, in case matrix is not sparse, it could be faster to just write values (but all of them):

    Out:

        dos.write(xSize);
        dos.write(ySize);
        for (int x=0; x<xSize; x++) {
            for (int y=0; y<ySize; y++) {
                value = matrix[x,y];
                dos.write(value);
            }
        }
    

    In:

       int xSize = dis.readInt();
       int ySize = dis.readInt();
       for (int x=0; x<xSize; x++) {
            for (int y=0; y<ySize; y++) {
                  double value = dis.readDouble();
                  matrix[x,y] = value;
            }
       }
    

    (mind I have not compiled it – so you might need to correct some stuff – it is out of the top of my head).

    Without buffers, you will read byte by byte which will make it slow.

    One more comment – with such a huge dataset, you should consider using SparseMatrix and write/read only the elements which are non-zero (unless you really have that many of significant elements).

    As wrote in the comment above – if you really want to write/read every single element in the matrix of that size, then you are already talking about hours of write rather than seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with creating a program that will generate an amortization schedule.
I have this program that I want other processes to be able to call
I have a program that uses XSL-FO (org.apache.fop.apps.Driver) to generate a PDF print out
At work, I have started working on a program that can potentially generate hundreds
I have a program that is using the excel interop routines to generate a
I have a program where I generate bitstreams, of about 80 to 150 bits
I used SWIG to generate a Perl module for a C++ program. I have
I have a program that generates text fiels that can be up to 20
I have a program that generates a .shp file, and exports this as a
I have a program that generates/'rolls' two dice. I would like to output these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.