c - 如何比较 Rcpp 和 C 的性能?

标签 c r rcpp

我希望能够比较从 R 运行一些 C 代码的性能(使用包 inlineRcpp)。我正在使用 rbenchmark 在 R 中执行此操作。下面是一个简单的示例:-

在 R 中我一直在使用:-

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))

这给出了输出:-

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

在 C 语言中我一直在使用相同的函数:-

#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;
}

文件 some_numbers.txt 只是一个包含 100,000 个随机数的文件,该文件是使用 R 中的 rnorm(100000) 生成的。

这给出了输出:-

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

那么,比较 rbenchmarkelapsed 时间和我从 C 函数得到的时间是否有意义?如果是这样,为什么使用 RcppinlineR 调用的相同函数显然比从 C 调用时表现更好?

最佳答案

为了比较事物,它们必须可比较。而且,一般来说,独立的 main() 实现不能与 R 托管某些东西相媲美,因为从内存分配到根据需要修改其他行为的框架完全不同R(和类似的脚本语言)。

您可以比较不同的算法,既可以由 R 调用,也可以作为 C 独立程序调用。

您可以比较由 R 调用或作为 C 独立程序调用的不同语言

但是你不能真正比较你做的方式。

关于c - 如何比较 Rcpp 和 C 的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397596/

相关文章:

java - rJava 泛型类型

R Shiny 包装 UI 元素

python - 是否有任何可用的python 寻路库?

c - 静态/全局枚举变量的初始值是多少?

c - 在c中传递一个二维子数组

r - 如何在 R/Shiny 中构建响应式(Reactive)数据框?

c++ - "2 duplicate symbols for architecture"编译包Rcpp、RcppProgress时

c++ - 静态大小的Rcpp空列表是否比list.push_back()更有效?

c++ - Rcpp 中 AR(1) 的模拟

c - 如何从文件中获取字符串并存储在二维字符数组中,并将该二维字符数组与 C 中的字符串进行比较?