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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:07:50+00:00 2026-06-14T08:07:50+00:00

I’d like to be able to compare the performance of running some C code

  • 0

I’d like to be able to compare the performance of running some C code from R (using package inline and Rcpp). I am using rbenchmark to do this in R. A trivial example follows:-

In R I have been using:-

library(inline)
## function to calculate a mean:-
mean_fun <- cxxfunction(signature(a = "numeric"), plugin = "Rcpp", body = '
  Rcpp::NumericVector xa(a);
    int n = xa.size();
    double sum = 0;
    for(int i = 0; i < n; i++) {
        sum += xa[i];
    }
  double mean = sum / n;
    return Rcpp::wrap(mean);    
')
x <- rnorm(100000)
require(rbenchmark)
print(benchmark(mean_fun(x), mean(x), 
      columns = c("test", "replications", "elapsed", "relative"),
      replications = 100))

This gives the output:-

         test replications elapsed relative
2 mean_fun(x)          100   0.019    1.000
1     mean(x)          100   0.039    2.053

And in C I have been essentially using the same function:-

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

#define CLOCKTYPE CLOCK_MONOTONIC
#define MAX_ROWS 1000010


struct Double_array_struct {
    int size_array;
    double double_array[MAX_ROWS];
};

struct Double_array_struct *Load_double_array_struct()
{
    /* loads some_numbers.txt and stores the data into an
    Double_array_struct, resizes the memory allocation, 
    returns the pointer to the struct */
    struct Double_array_struct *data = malloc(MAX_ROWS * sizeof(double) +
        sizeof(int));
    data->size_array = MAX_ROWS;
    double num = 0;
    int array_length = 0;

    FILE *myfile = fopen("some_numbers.txt", "r");
    if (myfile == NULL) {
        perror("error opening file");
    } else {
        printf("File successfully opened\n");
        while(fscanf(myfile, "%lf", &num) > 0)
        {
            /*printf("Number = %lf\n", num);*/
            data->double_array[array_length] = num;
            array_length++;
        }
        data->size_array = array_length;
    }
    fclose(myfile);
    printf("array_length: %d\n", array_length);

    printf("re-sizing the data\n");
    data = realloc(data, (array_length * sizeof(double) + sizeof(int)));
    return(data);
}

void Destroy_double_array_struct(struct Double_array_struct *data) {
    /* function to free the memory used for Int array struct */
    assert(data != NULL);
    free(data);
}

double Mean_double_array_struct(struct Double_array_struct *data) {
    /* function to calculate the mean of an array of 
    doubles, when passed to function in the usual structure.
    Returns a double */
    int i;
    double sum = 0; 
    for(i = 0; i < data->size_array; i++){
        sum += data->double_array[i];
    }
    double mean = sum / data->size_array;
    return(mean);
}


int main()
{

    struct Double_array_struct *double_file_data = Load_double_array_struct();

    /* some timings */    
    printf("about to do timings");
    struct timespec tsi, tsf;
    clock_gettime(CLOCKTYPE, &tsi);
    int z;
    int iterations = 100; /*100 iterations to match rbenchmark */
    double new_array_mean[iterations];
    for(z = 0; z < iterations; z++){
        new_array_mean[z] = Mean_double_array_struct(double_file_data);
        /* I've allocated the result to an array to stop the
        compiler complaining that we're never using the result
        of the function */ 
    }
    clock_gettime(CLOCKTYPE, &tsf);
    double elaps_s = difftime(tsf.tv_sec, tsi.tv_sec);
    long elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
    double time_taken = elaps_s + ((double)elaps_ns) / 1.0e9;

    printf("time taken %lf\n", time_taken);
    Destroy_double_array_struct(double_file_data);

    return 0;
}

The file some_numbers.txt is just a file of 100,000 random numbers generated with rnorm(100000) in R.

This gives the output:-

File successfully opened
array_length: 100000
re-sizing the data
about to do timings:
mean: 0.003486
time taken 0.095793

So, is it meaningful to compare the elapsed time from rbenchmark, and the time which I got from the C function? And if so, why does the same function called from R with Rcpp and inline apparently perform better than when it is called from C?

  • 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-14T08:07:51+00:00Added an answer on June 14, 2026 at 8:07 am

    In order to compare things, they have to be comparable. And, generally speaking, a standalone main() implementation is not comparable to R hosting something because of totally different frameworks from the memory allocation on down to modifying other behavior as needed by R (and comparable scripting languages).

    You could compare different algorithms both called by R, or as C standalone programs.

    You could compare different languages both called by R, or as C standalone programs.

    But you cannot really compare the way you did.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
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 have just tried to save a simple *.rtf file with some websites and

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.