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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:25:01+00:00 2026-05-15T17:25:01+00:00

I’m doing a benchmark with two versions of a function to filter a linked

  • 0

I’m doing a benchmark with two versions of a function to filter a linked list, one that receives a predicate function as an argument, and one in which the predicate is built into the function using a “macro template”. I would expect the first to run more slowly, because it is making a function call on every iteration, but on my box their speeds are about the same. Any clue about why is this happening? Any help is appreciated.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

struct list_t {
    int val;
    struct list_t *next;
};
typedef struct list_t list_t;

// Removes elements not matching the predicate.
// NOTE: This is not freeing the removed elements.

list_t *keep(int (*predfun)(int,int), int predarg, list_t *list) {
    list_t *this, *prev;

    for (this=list, prev=NULL; this; prev=this, this=this->next) {
        if (!predfun(this->val, predarg)) {
            if (!prev) {
                list = this->next;
            }
            else {
                prev->next = this->next;
            }
        }
    }

    return list;
}

int less(int a, int b) {
    return a<b;
}

// A "template" macro for the keep function.
// The idea is to embed the predicate into the function, so that we
// don't have to make a function call on each iteration.

#define build_keep(test) {                                           \
    list_t *this, *prev;                                             \
                                                                     \
    for (this=list, prev=NULL; this; prev=this, this=this->next) {   \
        if (!(test)) {                                               \
            if (!prev) {                                             \
                list = this->next;                                   \
            }                                                        \
            else {                                                   \
                prev->next = this->next;                             \
            }                                                        \
        }                                                            \
    }                                                                \
    return list;                                                     \
}


list_t *keep_less(int arg, list_t *list) {
    build_keep(this->val < arg);
}

#define LEN 1000000
#define MOD 1024

// Creates a new list.
list_t *buildlist() {
    int i;
    list_t *list, *last, *t;
    list=NULL, last=NULL;
    srand(0); // Using always the same seed for the benchmark.
    for (i=0; i<LEN; i++) {
        if (!last) {
            last = malloc(sizeof(list_t));
            list = last;
        }
        else {
            last->next = malloc(sizeof(list_t));
            last = last->next;
        }
        last->val = rand() % MOD;
    }
    last->next = NULL;
    return list;
}    

int main() {
    struct timeval t0, t1;
    list_t *list, *t;

    // With macro.
    list = buildlist();
    //for (t=list; t; t=t->next) printf("%d ", t->val); printf("\n");
    gettimeofday(&t0, NULL);
    keep_less(500, list);
    gettimeofday(&t1, NULL);
    printf("keep_less: %lf\n", (1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec))/1000000.0);
    //for (t=list; t; t=t->next) printf("%d ", t->val); printf("\n");

    printf("\n");

    // Without macro.
    list = buildlist();
    //for (t=list; t; t=t->next) printf("%d ", t->val); printf("\n");
    gettimeofday(&t0, NULL);
    keep(less, 500, list);
    gettimeofday(&t1, NULL);
    printf("keep: %lf\n", (1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec))/1000000.0);
    //for (t=list; t; t=t->next) printf("%d ", t->val); printf("\n");

    return 0;
}

The output here is:

keep_less: 0.181019

keep: 0.185590
  • 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-05-15T17:25:02+00:00Added an answer on May 15, 2026 at 5:25 pm

    I wouldn’t say the results are about the same – you do see a 4 milisecond (~2%) difference in favor of the immediate version.

    This is actually relatively substantial – it’s possible to achieve such a high saving with saving just a function call only because your test functions did very little to start with. If you expected more out of this optimization, you might have stumbled upon an important lesson… (that personally, I have to re-learn once every few weeks :] )

    • 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 am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.