Created
March 31, 2015 12:32
-
-
Save ytomino/2fa9272d342c4656b681 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__attribute__((noinline)) int c_sum(int *data, int n) | |
{ | |
int result = 0; | |
for(int i = 0; i < n; ++i){ | |
result += data[i]; | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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