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 8156765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:03:45+00:00 2026-06-06T17:03:45+00:00

I have an int and float array each of length 220 million (fixed). Now,

  • 0

I have an int and float array each of length 220 million (fixed). Now, I want to store/upload those arrays to/from memory and disk. Currently, I am using Java NIO’s FileChannel and MappedByteBuffer to solve this. It works fine, but it takes near about 5 seconds (Wall Clock Time) for storing/uploading array to/from memory to disk. Now, I want to make it faster.

Here, I should mention most of those array elements are 0 ( nearly 52 %).

like:

int arr1 [] = { 0 , 0 , 6 , 7 , 1, 0 , 0 ...}

Can anybody help me, is there any nice way to improve speed by not storing or loading those 0’s. This can compensated by using Arrays.fill (array , 0).

  • 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-06-06T17:03:46+00:00Added an answer on June 6, 2026 at 5:03 pm

    The following approach requires n / 8 + nz * 4 bytes on disk, where n is the size of the array, and nz the number of non-zero entries. For 52% zero entries, you’d reduce storage size by 52% – 3% = 49%.

    You could do:

    void write(int[] array) {
        BitSet zeroes = new BitSet();
        for (int i = 0; i < array.length; i++)
            zeroes.set(i, array[i] == 0);
        write(zeroes); // one bit per index
        for (int i = 0; i < array.length; i++)
            if (array[i] != 0)
                write(array[y]);
    }
    
    int[] read() {
        BitSet zeroes = readBitSet();
        array = new int[zeroes.length];
        for (int i = 0; i < zeroes.length; i++) {
            if (zeroes.get(i)) {
                // nothing to do (array[i] was initialized to 0)
            } else {
                array[i] = readInt();
            }
        }
    }
    

    Edit: That you say this is slightly slower implies that the disk is not the bottleneck. You could tune the above approach by writing the bitset as you construct it, so you don’t have to write the bitset to memory before writing it to disk. Also, by writing the bitset word by word interspersed with the actual data we can do only a single pass over the array, reducing cache misses:

    void write(int[] array) {
        writeInt(array.length);
        int ni;
        for (int i = 0; i < array.length; i = ni) {
            ni = i + 32;
            int zeroesMap = 0;
            for (j = i + 31; j >= i; j--) {
                zeroesMap <<= 1;
                if (array[j] == 0) {
                    zeroesMap |= 1;
                }
            }
            writeInt(zeroesMap);
            for (j = i; j < ni; j++)
                if (array[j] != 0) {
                    writeInt(array[j]);
                }
            }
        }
    }
    
    int[] read() {
        int[] array = new int[readInt()];
        int ni;
        for (int i = 0; i < array.length; i = ni) {
            ni = i + 32;
            zeroesMap = readInt();
            for (j = i; j < ni; j++) {
                if (zeroesMap & 1 == 1) {
                    // nothing to do (array[i] was initialized to 0)
                } else {
                    array[j] = readInt();
                }
                zeroesMap >>= 1;
            }
        }
        return array;
    }
    

    (The preceeding code assumes array.length is a multiple of 32. If not, write the last slice of the array in whatever way you like)

    If that doesn’t reduce proceccing time either, compression is not the way to go (I don’t think any general purpose compression algorithm will be faster than the above).

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

Sidebar

Related Questions

I have an int and float array of length 220 million (fixed). Now, I
I have a float array in java and want to convert each element to
Suppose I have a float[] where each position in the array has unique meaning.
I have a 1-dimensional float array of root mean square values, each calculated with
Say we have a struct of an int array, a float array etc. and
I have 1000 float datas in an array. I want to separate into different
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have this situation: { float foo[10]; for (int i = 0; i <
I have a table that contains the following columns: ID int, DISTANCE float, EVENT

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.