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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:10:57+00:00 2026-06-14T22:10:57+00:00

For an assignment of a course called High Performance Computing, I required to optimize

  • 0

For an assignment of a course called High Performance Computing, I required to optimize the following code fragment:

int foobar(int a, int b, int N)
{
    int i, j, k, x, y;
    x = 0;
    y = 0;
    k = 256;
    for (i = 0; i <= N; i++) {
        for (j = i + 1; j <= N; j++) {
            x = x + 4*(2*i+j)*(i+2*k);
            if (i > j){
               y = y + 8*(i-j);
            }else{
               y = y + 8*(j-i);
            }
        }
    }
    return x;
}

Using some recommendations, I managed to optimize the code (or at least I think so), such as:

  1. Constant Propagation
  2. Algebraic Simplification
  3. Copy Propagation
  4. Common Subexpression Elimination
  5. Dead Code Elimination
  6. Loop Invariant Removal
  7. bitwise shifts instead of multiplication as they are less expensive.

Here’s my code:

int foobar(int a, int b, int N) {

    int i, j, x, y, t;
    x = 0;
    y = 0;
    for (i = 0; i <= N; i++) {
        t = i + 512;
        for (j = i + 1; j <= N; j++) {
            x = x + ((i<<3) + (j<<2))*t;
        }
    }
    return x;
}

According to my instructor, a well optimized code instructions should have fewer or less costly instructions in assembly language level.And therefore must be run, the instructions in less time than the original code, ie calculations are made with::

execution time = instruction count * cycles per instruction

When I generate assembly code using the command: gcc -o code_opt.s -S foobar.c,

the generated code has many more lines than the original despite having made ​​some optimizations, and run-time is lower, but not as much as in the original code. What am I doing wrong?

Do not paste the assembly code as both are very extensive. So I’m calling the function “foobar” in the main and I am measuring the execution time using the time command in linux

int main () {
    int a,b,N;

    scanf ("%d %d %d",&a,&b,&N);
    printf ("%d\n",foobar (a,b,N));
    return 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-14T22:10:59+00:00Added an answer on June 14, 2026 at 10:10 pm

    Initially:

    for (i = 0; i <= N; i++) {
        for (j = i + 1; j <= N; j++) {
            x = x + 4*(2*i+j)*(i+2*k);
            if (i > j){
               y = y + 8*(i-j);
            }else{
               y = y + 8*(j-i);
            }
        }
    }
    

    Removing y calculations:

    for (i = 0; i <= N; i++) {
        for (j = i + 1; j <= N; j++) {
            x = x + 4*(2*i+j)*(i+2*k);
        }
    }
    

    Splitting i, j, k:

    for (i = 0; i <= N; i++) {
        for (j = i + 1; j <= N; j++) {
            x = x + 8*i*i + 16*i*k ;                // multiple of  1  (no j)
            x = x + (4*i + 8*k)*j ;                 // multiple of  j
        }
    }
    

    Moving them externally (and removing the loop that runs N-i times):

    for (i = 0; i <= N; i++) {
        x = x + (8*i*i + 16*i*k) * (N-i) ;
        x = x + (4*i + 8*k) * ((N*N+N)/2 - (i*i+i)/2) ;
    }
    

    Rewritting:

    for (i = 0; i <= N; i++) {
        x = x +         ( 8*k*(N*N+N)/2 ) ;
        x = x +   i   * ( 16*k*N + 4*(N*N+N)/2 + 8*k*(-1/2) ) ;
        x = x +  i*i  * ( 8*N + 16*k*(-1) + 4*(-1/2) + 8*k*(-1/2) );
        x = x + i*i*i * ( 8*(-1) + 4*(-1/2) ) ;
    }
    

    Rewritting – recalculating:

    for (i = 0; i <= N; i++) {
        x = x + 4*k*(N*N+N) ;                            // multiple of 1
        x = x +   i   * ( 16*k*N + 2*(N*N+N) - 4*k ) ;   // multiple of i
        x = x +  i*i  * ( 8*N - 20*k - 2 ) ;             // multiple of i^2
        x = x + i*i*i * ( -10 ) ;                        // multiple of i^3
    }
    

    Another move to external (and removal of the i loop):

    x = x + ( 4*k*(N*N+N) )              * (N+1) ;
    x = x + ( 16*k*N + 2*(N*N+N) - 4*k ) * ((N*(N+1))/2) ;
    x = x + ( 8*N - 20*k - 2 )           * ((N*(N+1)*(2*N+1))/6);
    x = x + (-10)                        * ((N*N*(N+1)*(N+1))/4) ;
    

    Both the above loop removals use the summation formulas:

    Sum(1, i = 0..n) = n+1
    Sum(i1, i = 0..n) = n(n + 1)/2
    Sum(i2, i = 0..n) = n(n + 1)(2n + 1)/6
    Sum(i3, i = 0..n) = n2(n + 1)2/4

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

Sidebar

Related Questions

The assignment at my first year uni computing course says my program should read
int value = 5; // this type of assignment is called an explicit assignment
In the Stanford iPhone course CS193P assignment 2 (free online course), the step through
I'm working on my assignment of PHP course. The problem that I don't understand
I'm doing a homework assignment for my course in C (first programming course). Part
I am currently completing my first java course and assignment based on a random
My current assignment in a beginning Java course is to animate a circle across
I am doing an assignment in a university course and I am using Git
In my software course, each time we submit an assignment, we have to include
I have to do a sniffer as an assignment for the security course. I

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.