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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:59:30+00:00 2026-05-26T02:59:30+00:00

I have to find a diagonal difference in a matrix represented as 2d array

  • 0

I have to find a diagonal difference in a matrix represented as 2d array and the function prototype is

int diagonal_diff(int x[512][512])

I have to use a 2d array, and the data is 512×512. This is tested on a SPARC machine: my current timing is 6ms but I need to be under 2ms.

Sample data:

[3][4][5][9]
[2][8][9][4]
[6][9][7][3]
[5][8][8][2]

The difference is:

|4-2| + |5-6| + |9-5| + |9-9| + |4-8| + |3-8| = 2 + 1 + 4 + 0 + 4 + 5 = 16

In order to do that, I use the following algorithm:

int i,j,result=0;
for(i=0; i<4; i++)
    for(j=0; j<4; j++)
        result+=abs(array[i][j]-[j][i]);

return result;

But this algorithm keeps accessing the column, row, column, row, etc which make inefficient use of cache.

Is there a way to improve my function?

  • 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-26T02:59:30+00:00Added an answer on May 26, 2026 at 2:59 am

    EDIT: Why is a block oriented approach faster? We are taking advantage of the CPU’s data cache by ensuring that whether we iterate over a block by row or by column, we guarantee that the entire block fits into the cache.

    For example, if you have a cache line of 32-bytes and an int is 4 bytes, you can fit a 8×8 int matrix into 8 cache lines. Assuming you have a big enough data cache, you can iterate over that matrix either by row or by column and be guaranteed that you do not thrash the cache. Another way to think about it is if your matrix fits in the cache, you can traverse it any way you want.

    If you have a matrix that is much bigger, say 512×512, then you need to tune your matrix traversal such that you don’t thrash the cache. For example, if you traverse the matrix in the opposite order of the layout of the matrix, you will almost always miss the cache on every element you visit.

    A block oriented approach ensures that you only have a cache miss for data you will eventually visit before the CPU has to flush that cache line. In other words, a block oriented approach tuned to the cache line size will ensure you don’t thrash the cache.

    So, if you are trying to optimize for the cache line size of the machine you are running on, you can iterate over the matrix in block form and ensure you only visit each matrix element once:

    int sum_diagonal_difference(int array[512][512], int block_size)
    {
        int i,j, block_i, block_j,result=0;
    
         // sum diagonal blocks
        for (block_i= 0; block_i<512; block_i+= block_size)
            for (block_j= block_i + block_size; block_j<512; block_j+= block_size)
                for(i=0; i<block_size; i++)
                    for(j=0; j<block_size; j++)
                        result+=abs(array[block_i + i][block_j + j]-array[block_j + j][block_i + i]);
    
        result+= result;
    
         // sum diagonal
        for (int block_offset= 0; block_offset<512; block_offset+= block_size)
        {
            for (i= 0; i<block_size; ++i)
            {
                for (j= i+1; j<block_size; ++j)
                {
                    int value= abs(array[block_offset + i][block_offset + j]-array[block_offset + j][block_offset + i]);
                    result+= value + value;
                }
            }
        }
    
        return result;
    }
    

    You should experiment with various values for block_size. On my machine, 8 lead to the biggest speed up (2.5x) compared to a block_size of 1 (and ~5x compared to the original iteration over the entire matrix). The block_size should ideally be cache_line_size_in_bytes/sizeof(int).

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

Sidebar

Related Questions

I have a Find function in order to find an element from a BST
I have the following 2d array int [][] array = {{ 0, 1, 2,
I don't have enough memory to simply create a diagonal D-by-D matrix, since D
I am evaluating datamining packages. I have find these two so far: RapidMiner Weka
I have to find the IDs of all the contentPlaceHolders in a MasterPage.
I have a few models that need to have custom find conditions placed on
I have two Java instances of java.util.Date and I have to find out if
Nowadays most of the Restaurants and other businesses have a Find Locations functionality on
I'm having some trouble with an SQL statement that have to find the number
I am working on a project where I have to find out the keyword

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.