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

The Archive Base Latest Questions

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

So I have a random question when coding the image processing function that involves

  • 0

So I have a random question when coding the image processing function that involves time complexity. The following is my original snippet of code:

    long start = System.currentTimeMillis();

    for (int i = 0; i < newWidth; i++) {
        for (int j = 0; j < newHeight; j++) {

            double x = i * scaleX;
            double y = j * scaleY;

            double xdiff = x - (int) x;
            double ydiff = y - (int) y;

            int xf = (int) Math.floor(x);
            int xc = (int) Math.ceil(x);
            int yf = (int) Math.floor(y);
            int yc = (int) Math.ceil(y);

            double out = inputArray[xf][yf] * (1 - xdiff) * (1 - ydiff)
                    + inputArray[xc][yf] * xdiff * (1 - ydiff)
                    + inputArray[xf][yc] * (1 - xdiff) * ydiff
                    + inputArray[xc][yc] * xdiff * ydiff;

            outputArray[i][j] = (int) out;
        }
    }

    long elapsed = System.currentTimeMillis() - start;
    System.out.println("Time used: " + elapsed);

And after coming out with that code, I was wondering whether it would be faster not to create 4 temporary variables for floor and ceiling values but, instead, calculate them directly in array indexing. So I modified it this way:

    long start = System.currentTimeMillis();

    for (int i = 0; i < newWidth; i++) {
        for (int j = 0; j < newHeight; j++) {

            double x = i * scaleX;
            double y = j * scaleY;

            double xdiff = x - (int) x;
            double ydiff = y - (int) y;

            double out = inputArray[(int) Math.floor(x)][(int) Math.floor(y)] * (1 - xdiff) * (1 - ydiff)
                    + inputArray[(int) Math.ceil(x)][(int) Math.floor(y)] * xdiff * (1 - ydiff)
                    + inputArray[(int) Math.floor(x)][(int) Math.ceil(y)] * (1 - xdiff) * ydiff
                    + inputArray[(int) Math.ceil(x)][(int) Math.ceil(y)] * xdiff * ydiff;

            outputArray[i][j] = (int) out;
        }
    }

    long elapsed = System.currentTimeMillis() - start;
    System.out.println("Time used: " + elapsed);

I was expecting the later to be faster (because you don’t have to write to a temporary variable and then access it) but it turned out that the later is, at least, 2.5 times slower than the former code. Test case used is 3x zoom of 1024×768 img.

former code: Time used: 812
later code: Time used: 2140

So what is cause of the time difference?

  • 1 1 Answer
  • 2 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:36:15+00:00Added an answer on May 26, 2026 at 1:36 am

    Q: How many times are you calling “floor” and “ceil” in the first case? The second case? 😉

    Q: I’m wondering if this would run any faster than the first case:

        long start = System.currentTimeMillis();
    
        for (int i = 0; i < newWidth; i++) {
            double x = i * scaleX;
            double xdiff = x - (int) x;
            int xf = (int) Math.floor(x);
            int xc = (int) Math.ceil(x);
            for (int j = 0; j < newHeight; j++) {
                double y = j * scaleY;
                double ydiff = y - (int) y;
    
                int yf = (int) Math.floor(y);
                int yc = (int) Math.ceil(y);
    
                double out = inputArray[xf][yf] * (1 - xdiff) * (1 - ydiff)
                        + inputArray[xc][yf] * xdiff * (1 - ydiff)
                        + inputArray[xf][yc] * (1 - xdiff) * ydiff
                        + inputArray[xc][yc] * xdiff * ydiff;
    
                outputArray[i][j] = (int) out;
            }
        }
    
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("Time used: " + elapsed);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code on my PHP powered site that creates a random hash
So, I have the following line of code, that does a for loop and
I have the following database design question: I have a record that contains the
I have a pseudo random number generator (PRNG) class that I want to unit
I have a program that uses the mt19937 random number generator from boost::random. I
I have a webpart that renders random list items (from any list and list
I have an external JavaScript that contains: function random_imglink(){ var myimages=new Array() myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png" myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
This is not a coding question, but am hoping that someone has come across
I have a random question. If I were to do a sql select and
I have a website that grabs a random entry from a database and displays

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.