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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:43:08+00:00 2026-06-10T10:43:08+00:00

I’m trying to implement an image processing function. Here it is: typedef void (*AgFilter)(int*,

  • 0

I’m trying to implement an image processing function. Here it is:

typedef void (*AgFilter)(int*, int*, int*, float*);

static void filter(AndroidBitmapInfo* info, void* pixels, AgFilter func, void* params){

    for(y = 0; y < height; y++){
        for(x = 0; x < width; x++){
            //initizalie r, g, b

            func(&r, &g, &b, params); //here is the problem
        }
    }
}

I’m passing this function as func:

static inline void brightness(int *r, int *g, int *b, float* param){
    float add = param[0];

    *r += add;
    *g += add;
    *b += add;
}

Problem that it’s works extremly slow. Well, I can understand that. But if instead of passing function by reference I write my function directly inside the filter (instread of func calling) it’s works much much faster. Where is the problem?

P.S. note that it’s not c++

EDIT

This one works fast:

static void filter(AndroidBitmapInfo* info, void* pixels, int add){

    for(y = 0; y < height; y++){
        for(x = 0; x < width; x++){
            //initizalie r, g, b
            r += add;
            g += add;
            b += add;
        }
    }
}
  • 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-10T10:43:10+00:00Added an answer on June 10, 2026 at 10:43 am

    Calling functions takes time. Usually, you don’t notice but you call that function a million times (about two million times for a full HD 1920×1080 image). Modern cameras create 16 Megapixel images. If each call takes 1 us, the accumulated time for calling the function (without actually executing the body) will be 16 seconds.

    How can you make it faster? Some suggestions:

    1. Instead of passing four parameters, use a struct:

       struct data { int r,g,b; float* param; }
      

      allocate this once and reuse it. Now you can call func with a single argument.

    2. Memory layout might be a problem. param is anywhere in memory. Copy it into struct data instead:

       struct data { int r,g,b, add; }
      

      The reason for this is that param is anywhere in memory which means it’s probably in a different cache line. If you can fit all the data into a single 64 byte structure, all will fit into a single cache line which can give a huge performance boost.

      But probably not in your case since you always access param[0]. This is more an issue when you would access the array in a random way.

    3. Swap shift and bit mask operations:

       r = (int) ((line[x] & >> 16 ) & 0xFF);
      

      Can give a small boost since all three colors will now be masked with 0xFF and that allows the compiler to move the constant once to a CPU register.

    4. When calling functions, all the CPU registers need to be “saved/restored”. That costs time. When the function is inlined, the compiler knows which CPU registers is trashed and can optimize accordingly.

      Actually, the CPU registers aren’t saved (at least I haven’t seen that for a long time). Modern compilers just assumes that after calling the function, all of them have been changed.

    5. Note that inline has no effect since you pass the function by reference instead of directly calling it.

    6. Use threads. This is dead simple to parallelize: Run the function N times (one per CPU core) on 1/N-th of the data. That will give you roughly a N time performance boost.

    • 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
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
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'm trying to select an H1 element which is the second-child in its group
I need a function that will clean a strings' special characters. I do NOT

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.