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

  • Home
  • SEARCH
  • 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 847769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:50:22+00:00 2026-05-15T06:50:22+00:00

I have three questions about three nested loops: for (int x=0; x<400; x++) {

  • 0

I have three questions about three nested loops:

for (int x=0; x<400; x++)
{
    for (int y=0; y<300; y++)
    {
        for (int z=0; z<400; z++)
        {
             // compute and store value
        }
    }
}

And I need to store all computed values. My standard approach would be to use a 3D-array:

values[x][y][z] = 1; // test value

but this turns out to be slow: it takes 192 ms to complete this loop, where a single int-assignment

int value = 1; // test value

takes only 66 ms.

1) Why is an array so relatively slow?
2) And why does it get even slower when I put this in the inner loop:

values[z][y][x] = 1; // (notice x and z switched)

This takes more than 4 seconds!

3) Most importantly: Can I use a data structure that is as quick as the assignment of a single integer, but can store as much data as the 3D-array?

  • 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-15T06:50:23+00:00Added an answer on May 15, 2026 at 6:50 am

    1) Why is an array so relatively slow?

    As others pointed, you are comparing apples to oranges. The triple-array is slow because it needs to dereference (internally at least – yes, “there are no pointers in Java”) three times; but then again, you cannot reference a single integer variable…

    2) And why does it get even slower when I put this in the inner loop:

    values[z][y][x] = 1; // (notice x and z switched)
    

    Because you have decreased cache coherence. The fastest-changing indices should be the last ones, so that most memory accesses occur next to each other, within the same cache blocks, instead of forcing your processor to wait until the blocks are read from the main RAM.

    3) Most importantly: Can I use a data structure that is as quick as the assignment of a single integer, but can store as much data as the 3D-array?

    No. There is no such structure, since the integer variable fits into a machine register (quicker even than the processor’s memory cache), and can always be accessed faster than anything else you care to mention. Processor speeds are much, much faster then main memory speeds. If your ‘working set’ (the data that you need to operate on) does not fit into registers or cache, you will have to pay a penalty to fetch it from RAM (or even worse, disk).

    This being said, Java does boundary checks on each array access, and does not seem to be too smart about optimizing the boundary checks away. The following comparison may be of interest:

    public static long test1(int[][][] array) {
        long start = System.currentTimeMillis();
        for ( int x = 0; x < 400; x++ ) {
            for ( int y = 0; y < 300; y++ ) {
                for ( int z = 0; z < 400; z++ ) {
                    array[x][y][z] = x + y + z;
                }
            }
        }
        return System.currentTimeMillis() - start;
    }
    
    public static long test2(int [] array) {
        long start = System.currentTimeMillis();
        for ( int x = 0; x < 400; x++ ) {
            for ( int y = 0; y < 300; y++ ) {
                for ( int z = 0; z < 400; z++ ) {
                    array[z + y*400 + x*400*300] = x + y + z;
                }
            }
        }
        return System.currentTimeMillis() - start;
    }
    
    public static void main(String[] args) {
    
        int[][][] a1 = new int[400][300][400];
        int[] a2 = new int[400*300*400];
        int n = 20;
    
        System.err.println("test1");
        for (int i=0; i<n; i++) {
            System.err.print(test1(a1) + "ms ");
        }
        System.err.println();
        System.err.println("test2");
        for (int i=0; i<n; i++) {
            System.err.print(test2(a2) + "ms ");
        }
        System.err.println();
    }
    

    The output, on my system, is

    test1
    164ms 177ms 148ms 149ms 148ms 147ms 150ms 151ms 152ms 154ms 151ms 150ms 148ms 148ms 150ms 148ms 150ms 148ms 148ms 149ms 
    test2
    141ms 153ms 130ms 130ms 130ms 133ms 130ms 130ms 130ms 132ms 129ms 131ms 130ms 131ms 131ms 130ms 131ms 130ms 130ms 130ms
    

    Therefore, there is some room for improvement… but I really don’t think it is worth your while.

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

Sidebar

Related Questions

There have been some questions about whether or not JavaScript is an object-oriented language.
There have been several questions already posted with specific questions about dependency injection ,
OK, I know there have already been questions about getting started with TDD ..
There have been several questions recently about database indexing and clustered indexing and it
I know there have been questions in the past about SQL 2005 versus Lucene.NET
I have three easy questions. Does anybody use QuickTest Pro for automated testing? Any
Some time ago I asked a question about nested loops on SO and as
PROBLEM I have a nested PHP array that I need to populate from flat
I have a question about this question . I posted a reply there but
I have a simple question about Java's XML API and I hope there's a

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.