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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:10:08+00:00 2026-05-26T01:10:08+00:00

This code (A) executes much faster (10 times) then the second one: for(int w=0;

  • 0

This code (A) executes much faster (10 times) then the second one:

for(int w=0; w<width; w++) {
        for(int h=1; h<height; h++) {
            image[h][w] = (1-a)*image[h][w] + a*image[h-1][w];
        }
    }

Second one:

for(int h=0; h<height; h++) {
        for(int w=1; w<width; w++) {
            image[h][w] = (1-a)*image[h][w] + a*image[h][w-1];
        }
    }

Why is that? it is the same to go through all of the pixels in the image in either horizontal or vertical direction.

Is there a way to speed up the second one?

Thanks in advance.

  • 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-26T01:10:08+00:00Added an answer on May 26, 2026 at 1:10 am

    This has to do with locality of reference. If you access the elements in the same order as they are stored in memory, this will be much faster than accessing them in a strided pattern, as the memory caches and memory bandwidth will be utilized far more effectively.

    The above would explain the second version being faster than the first one, and this is exactly what happens on my box:

    aix@aix:~$ time ./ver1
    real    0m29.421s
    
    aix@aix:~$ time ./ver2
    real    0m2.198s
    

    Here is the code I use to allocate the array:

      double a = 0.5;
      int width = 2048;
      int height = 2048;
      double* data = new double[height * width];
      double** image = new double*[height];
      for (int i = 0; i < height; i++) {
        image[i] = data + i * width;
      }
    

    Version 1 times the following loop:

      for (int iter = 0; iter < 100; iter++) {
        for(int w=0; w<width; w++) {
          for(int h=1; h<height; h++) {
            image[h][w] = (1-a)*image[h][w] + a*image[h-1][w];
          }
        }
      }
    

    Version 2 loop:

      for (int iter = 0; iter < 100; iter++) {
        for(int h=0; h<height; h++) {
          for(int w=1; w<width; w++) {
            image[h][w] = (1-a)*image[h][w] + a*image[h][w-1];
          }
        }
      }
    

    Compiled with g++ 4.4.3 with -O3 and run on a Xeon box of some description (64-bit Ubuntu).

    If you are still 100% sure that you’re seeing the opposite effect, there must be something fundamentally different about what you’re doing compared to what I’m doing. It might help if you told us the dimensions of your image and how exactly it gets allocated (in order to help establish the memory layout).

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

Sidebar

Related Questions

Considering this code, can I be absolutely sure that the finally block always executes,
I am getting this error on a remote server, but the same code executes
This code executes handbrakecli (a command line application) and places the output into a
This bit of code is taking almost a half second to execute. Could somebody
I have this code: void fill_array (unsigned int *iarray, char *string, int max) {
I want to make this code run, but I'm still too much of a
This code is executed by many way. When it's executed by the form button
With doctrine if you execute this code $columns = $accountTable->getColumns(); foreach ($columns as $column)
If I run this code, will each AppDomain execute in a different thread? ThreadPool.QueueUserWorkItem(delegate
With this code: var db = google.gears.factory.create('beta.database'); db.open('cominar'); db.execute('CREATE TABLE IF NOT EXISTS Ajax

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.