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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:46:22+00:00 2026-06-12T13:46:22+00:00

The code below executes 4x faster, if near REPLACING WITH .. line, the functor

  • 0

The code below executes 4x faster, if near “REPLACING WITH ..” line, the functor compare_swaps() will be replaced with direct reference to my_intcmp(). Apparently the indirect use is not being inlined. Why?

inline bool my_intcmp( const int & a, const int & b ) {
        return a < b;
}


template <class T, class compare>
void insertion_sort_3( T* first, T* last, compare compare_swaps ) {
    // Count is 1?
    if( 1 == (last - first) )
        return;

    for( T* it = first + 1; it != last; it ++ ) {
        T val = *it;
        T* jt;

        for( jt = it; jt != first; jt -- ) {
            // REPLACING WITH if( my_intcmp(val, *(jt - 1) ) gives 4x speedup, WHY?
            if( compare_swaps(val, *(jt - 1)) ) {
                *jt = *(jt - 1);
            } else {
                break;
            }
        }

        *jt = val;
    }
}

#define SIZE 100000
#define COUNT 4

int main() {
    int myarr[ SIZE ];

    srand( time(NULL) );

    int n = COUNT;
    while( n-- ) {
        for( int i = 0; i < SIZE; i ++ )
            myarr[ i ] = rand() % 20000;

        insertion_sort_3( myarr, myarr + SIZE, my_intcmp );
    }

    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-12T13:46:23+00:00Added an answer on June 12, 2026 at 1:46 pm

    The compiler sees a function pointer which he can’t really determine as not changing. I have seen this a couple of times before. The fix to the problem is to use a simple wrapper struct:

    struct my_intcmp_wrapper
    {
        bool operator()(int v0, int v1) const {
            return my_intcmp(v0, v1);
        }
    };
    

    Specifically for build-in types you probably want to pass the objects by value rather than by reference. For inlined functions it probably doesn’t make much of a difference but if the function isn’t inlined it generally makes the situation worse.

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

Sidebar

Related Questions

The code below fails on the line: Class.forName(oracle.jdbc.driver.OracleDriver); with the error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver The
If you run the code below it actually executes the finally after every call
The code below seemingly executes the web service and returns values, but ignores the
I have the code below which loops through selected checkboxes and executes and sql
In the C++ code below, am I guaranteed that the ~obj() destructor will be
The below code executes, but it only extracts an empty bitmap file. Any ideas
The code below will output: Event published Command executed Using System.Transactions how can I
I have written the java code below, which executes another java program named Newsworthy_RB.
The JavaScript code below is executed every time the user selects the login button
The code below works well right now; however, it's serially executed. I'd like to

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.