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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:49:27+00:00 2026-06-14T19:49:27+00:00

I’m optimizing the function, I try every way and even sse, and modified code

  • 0

I’m optimizing the function, I try every way and even sse, and modified code to return from different position to see the calculate timespan but finally I found most of the time spends on the bool judgement. Even I replace all code in the if statement with a simple add operation in it, it still cost 6000ms.

My platform is gcc 4.7.1 e5506 cpu. Its input ‘a’ and ‘b’ is a 1000size int array, and ‘asize’, ‘bsize’ are corresponding array size. MATCH_MASK = 16383, I run the function 100000 times to statistics a timespan. Is there any good idea to the problem. Thank you!

   if (aoffsets[i] && boffsets[i])  // this line costs most time

Code:

uint16_t aoffsets[DOUBLE_MATCH_MASK] = {0}; // important! or it will only be right on the first time
uint16_t* boffsets = aoffsets + MATCH_MASK;
uint8_t* seen = (uint8_t *)aoffsets;
auto fn_init_offsets = [](const int32_t* x, int n_size, uint16_t offsets[])->void
{
    for (int i = 0; i < n_size; ++i)
        offsets[MATCH_STRIP(x[i])] = i;
};
fn_init_offsets(a, asize, aoffsets);
fn_init_offsets(b, bsize, boffsets);

uint8_t topcount = 0;
int topoffset = 0;
{
    std::vector<uint8_t> count_vec(asize + bsize + 1, 0);   // it's the fastest way already, very near to tls
    uint8_t* counts = &(count_vec[0]);
            //return aoffsets[0];   // cost 1375 ms
    for (int i = 0; i < MATCH_MASK; ++i)
    {
        if (aoffsets[i] && boffsets[i])  // this line costs most time
        {
                            //++affsets[i];  // for test
            int offset = (aoffsets[i] -= boffsets[i]);
            if ((-n_maxoffset <= offset && offset <= n_maxoffset))
            {
                offset += bsize;
                uint8_t n_cur_count = ++counts[offset];
                if (n_cur_count > topcount)
                {
                    topcount = n_cur_count;
                    topoffset = offset;
                }
            }
        }
    }
}
    return aoffsets[0];   // cost 6000ms
  • 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-14T19:49:28+00:00Added an answer on June 14, 2026 at 7:49 pm

    You can increase the speed of your program by reducing the cache misses: aoffsets[i] and boffsets[i] are relatively far away from each other in memory. By placing them next to each other, you speed up the program significantly. On my machine (e5400 cpu, VS2012) the execution time is reduced from 3.0 seconds to 2.3 seconds:

    #include <vector>
    
    #include <windows.h> 
    #include <iostream> 
    
    
    typedef unsigned short uint16_t;
    typedef int int32_t;
    typedef unsigned int uint32_t;
    typedef unsigned char uint8_t;
    
    #define MATCH_MASK 16383
    #define DOUBLE_MATCH_MASK (MATCH_MASK*2)
    static const int MATCH_BITS = 14; 
    static const int MATCH_LEFT = (32 - MATCH_BITS); 
    #define MATCH_STRIP(x) ((uint32_t)(x) >> MATCH_LEFT)
    
    static const int n_maxoffset = 1000;
    
    uint16_t test(int32_t* a, int asize, int32_t* b, int bsize)
    {
        uint16_t offsets[DOUBLE_MATCH_MASK] = {0}; 
    
        auto fn_init_offsets = [](const int32_t* x, int n_size, uint16_t offsets[])->void
        {
            for (int i = 0; i < n_size; ++i)
                offsets[MATCH_STRIP(x[i])*2 /*important. leave space for other offsets*/] = i;
        };
        fn_init_offsets(a, asize, offsets);
        fn_init_offsets(b, bsize, offsets+1);
    
        uint8_t topcount = 0;
        int topoffset = 0;
        {
            std::vector<uint8_t> count_vec(asize + bsize + 1, 0);   
            uint8_t* counts = &(count_vec[0]);
            for (int i = 0; i < MATCH_MASK; i+=2)
            {
                if (offsets[i] && offsets[i+1])  
                {
                    int offset = (offsets[i] - offsets[i+1]); //NOTE: I removed 
                    if ((-n_maxoffset <= offset && offset <= n_maxoffset))
                    {
                        offset += bsize;
                        uint8_t n_cur_count = ++counts[offset];
                        if (n_cur_count > topcount)
                        {
                            topcount = n_cur_count;
                            topoffset = offset;
                        }
                    }
                }
            }
        }
        return offsets[0];   
    }
    
    
    int main(int argc, char* argv[])
    {
        const int sizes = 1000;
        int32_t* a = new int32_t[sizes];
        int32_t* b = new int32_t[sizes];
        for (int i=0;i<sizes;i++)
        {
            a[i] = rand()*rand();
            b[i] = rand()*rand();
        }
    
        //Variablen 
        LONGLONG g_Frequency, g_CurentCount, g_LastCount; 
    
        QueryPerformanceFrequency((LARGE_INTEGER*)&g_Frequency);
        QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); 
    
        int sum = 0;
    
        for (int i=0;i<100000;i++)
        {
            sum += test(a,sizes,b,sizes);
        }
    
        QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount); 
        double dTimeDiff = (((double)(g_LastCount-g_CurentCount))/((double)g_Frequency)); 
    
        std::cout << "Result: " << sum << std::endl <<"time: " << dTimeDiff << std::endl; 
    
    
        delete[] a;
        delete[] b;
        return 0;
    }
    

    compared to your version of test().

    #include <vector>
    
    #include <windows.h> 
    #include <iostream> 
    
    
    typedef unsigned short uint16_t;
    typedef int int32_t;
    typedef unsigned int uint32_t;
    typedef unsigned char uint8_t;
    
    #define MATCH_MASK 16383
    #define DOUBLE_MATCH_MASK (MATCH_MASK*2)
    static const int MATCH_BITS = 14; 
    static const int MATCH_LEFT = (32 - MATCH_BITS); 
    #define MATCH_STRIP(x) ((uint32_t)(x) >> MATCH_LEFT)
    static const int n_maxoffset = 1000;
    
    uint16_t test(int32_t* a, int asize, int32_t* b, int bsize)
    {
        uint16_t aoffsets[DOUBLE_MATCH_MASK] = {0}; // important! or it will only be right on the first time
        uint16_t* boffsets = aoffsets + MATCH_MASK;
    
        auto fn_init_offsets = [](const int32_t* x, int n_size, uint16_t offsets[])->void
        {
            for (int i = 0; i < n_size; ++i)
                offsets[MATCH_STRIP(x[i])] = i;
        };
        fn_init_offsets(a, asize, aoffsets);
        fn_init_offsets(b, bsize, boffsets);
    
        uint8_t topcount = 0;
        int topoffset = 0;
        {
            std::vector<uint8_t> count_vec(asize + bsize + 1, 0);   
            uint8_t* counts = &(count_vec[0]);
    
            for (int i = 0; i < MATCH_MASK; ++i)
            {
                if (aoffsets[i] && boffsets[i]) 
                {
    
                    int offset = (aoffsets[i] - boffsets[i]); //NOTE: I removed the -= because otherwise offset would always be positive!
                    if ((-n_maxoffset <= offset && offset <= n_maxoffset))
                    {
                        offset += bsize;
                        uint8_t n_cur_count = ++counts[offset];
                        if (n_cur_count > topcount)
                        {
                            topcount = n_cur_count;
                            topoffset = offset;
                        }
                    }
                }
            }
        }
        return aoffsets[0];   
    }
    
    
    int main(int argc, char* argv[])
    {
        const int sizes = 1000;
        int32_t* a = new int32_t[sizes];
        int32_t* b = new int32_t[sizes];
        for (int i=0;i<sizes;i++)
        {
            a[i] = rand()*rand();
            b[i] = rand()*rand();
        }
    
        LONGLONG g_Frequency, g_CurentCount, g_LastCount; 
        QueryPerformanceFrequency((LARGE_INTEGER*)&g_Frequency);
        QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount); 
    
        int sum = 0;
    
        for (int i=0;i<100000;i++)
        {
            sum += test(a,sizes,b,sizes);
        }
    
        QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount); 
    
        double dTimeDiff = (((double)(g_LastCount-g_CurentCount))/((double)g_Frequency)); 
    
        std::cout << "Result: " << sum << std::endl <<"time: " << dTimeDiff << std::endl; 
    
    
        delete[] a;
        delete[] b;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.