Thursday, 15 April 2010

"Generic" function in C - use same time-measuring function for 2 different libraries -



"Generic" function in C - use same time-measuring function for 2 different libraries -

i found here, on so, simple function measure execution time of function in c:

#include <stdio.h> #include <stdint.h> #include <time.h> int64_t timespecdiff(struct timespec *timea_p, struct timespec *timeb_p) { homecoming ((timea_p->tv_sec * 1000000000) + timea_p->tv_nsec) - ((timeb_p->tv_sec * 1000000000) + timeb_p->tv_nsec); } int main(int argc, char **argv) { struct timespec start, end; clock_gettime(clock_monotonic, &start); // code interested in measuring clock_gettime(clock_monotonic, &end); uint64_t timeelapsed = timespecdiff(&end, &start); }

now utilize measure cryptographic function executions. on, im interesting in openssl , crypto++ (but extend list other libraries).

in c++ utilize templates this, how c? let's say, have 2 functions:

void md4_openssl(...); , void md4_cryptopp(...); parameters setups (keys, buffers, ...) within functions. now, how can create measruring function more "generic"? in way possible:

void measuretime() { function(); // md4_openssl or md4_cryptopp }

of course, utilize opnessl if installed, if not, phone call cryptopp function

the obvious way pass pointer function measuretime():

uint64_t measuretime(void (*fp)(void)) { struct timespec start, end; clock_gettime(clock_monotonic, &start); fp(); // phone call function want measure clock_gettime(clock_monotonic, &end); homecoming timespecdiff(&end, &start); } void measure_ssl(void) { md4_openssl(1, 2, 3, whatever); // setup , phone call need } void measure_crypt(void) { md4_cryptopp(1, 2, 3, whatever); // setup , phone call need } int main(void) { uint64_t t1 = measuretime(measure_ssl); uint64_t t2 = measuretime(measure_crypt); // t1 , t2 homecoming 0; }

edit: in response comment, function pointer pass function must have signature matching parameter. if need phone call functions have different signatures, need wrap them above code does.

if wrapper function can take care of parameters, can utilize above code without modification. in other words, measure_ssl() can phone call md4_openssl(1, 2, 3, "more", "args") , measure_crypt() can phone call md4_cryptopp(5.4, "different", args) or whatever.

if need provide arguments measuretime(), you'll have pass info wrapper functions , have them interpretation. if each function took, say, 1 pointer, of different type, pass single void * , pass on.

if arguments can different , unpredictable, simplest way create struct , pass address of that, instance:

#include <stdio.h> #include <stdint.h> #include <inttypes.h> #include <time.h> void func1(int a, int b, int c) { printf("let's count %d, %d , %d!\n", a, b, c); } struct func1_params { int a; int b; int c; }; void func2(double d, const char * c) { printf("there %f %ss\n", d, c); } struct func2_params { double d; const char * c; }; void func1_wrap(void * params) { struct func1_params * p = params; func1(p->a, p->b, p->c); } void func2_wrap(void * params) { struct func2_params * p = params; func2(p->d, p->c); } int64_t timespecdiff(struct timespec *timea_p, struct timespec *timeb_p) { homecoming ((timea_p->tv_sec * 1000000000) + timea_p->tv_nsec) - ((timeb_p->tv_sec * 1000000000) + timeb_p->tv_nsec); } uint64_t measuretime(void (*fp)(void *), void * params) { struct timespec start, end; clock_gettime(clock_monotonic, &start); fp(params); clock_gettime(clock_monotonic, &end); homecoming timespecdiff(&end, &start); } int main(void) { struct func1_params p1 = {1, 2, 3}; uint64_t t1 = measuretime(func1_wrap, &p1); struct func2_params p2 = {3.14159, "pie"}; uint64_t t2 = measuretime(func2_wrap, &p2); printf("first func took time %" priu64 " units.\n", t1); printf("second func took time %" priu64 " units.\n", t2); homecoming 0; }

this generic, in measuretime() doesn't need know functions you're calling, or struct. compile library, , much later write, say, func1_wrap() , struct func1_params , pass them in. can measure time of function writing struct contain arguments , wrapper function of type void (*)(void *) take pointer struct , pass members arguments function you're interested in.

in above code, i've provided definitions of func1() , func2() provide finish working example, can think of these md4_openssl() , md4_cryptopp() functions instead, , have wrapper functions phone call those.

c

No comments:

Post a Comment