Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created March 31, 2015 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/2fa9272d342c4656b681 to your computer and use it in GitHub Desktop.
Save ytomino/2fa9272d342c4656b681 to your computer and use it in GitHub Desktop.
__attribute__((noinline)) int c_sum(int *data, int n)
{
int result = 0;
for(int i = 0; i < n; ++i){
result += data[i];
}
return result;
}
integer(c_int) function f_sum(arr, size) bind(c)
use iso_c_binding
implicit none
type(c_ptr), value :: arr
integer(c_int), value :: size
integer(c_int), dimension(:), pointer :: p
call c_f_pointer(arr, p, [size])
f_sum = sum(p)
end
#include <stdio.h>
#include <sys/resource.h>
extern int c_sum(int *, int);
extern int f_sum(int *, int);
//#define sum c_sum
#define sum f_sum
enum { c = 2 << 20 };
volatile int result[c];
int main()
{
struct rusage r1, r2;
int const n = 2 << 12;
int data[n];
getrusage (RUSAGE_SELF, &r1);
for(int i = 0; i < c; ++i){
result[i] = sum(data, n);
}
getrusage (RUSAGE_SELF, &r2);
long double t1 =
r1.ru_utime.tv_sec
+ (long double)r1.ru_utime.tv_usec / 1000000000;
long double t2 =
r2.ru_utime.tv_sec
+ (long double)r2.ru_utime.tv_usec / 1000000000;
printf("%Lf\n", t2 - t1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment