//util.cpp #include "model/util.h" void util::invoke_func_args(const util &ul, const int &len) { util ulcopy=ul; pass_func_args(ulcopy,len,&get_str_uuid2); pass_func_args(ulcopy,len,&get_char_uuid); } void util::pass_func_args(util&ul,const int&len,void(*func)(util&,const int&)) { std::chrono::time_point<std::chrono::high_resolution_clock> start_time,end_time; start_time=std::chrono::high_resolution_clock::now(); func(ul,len); end_time=std::chrono::high_resolution_clock::now(); std::cout<<"Time cost "<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count()<<" milliseconds!"<<std::endl<<std::endl; } void util::get_str_uuid2(util &ul,const int& len) { for(int i=0;i<len;i++) { ul.get_str_uuid(); } } void util::get_char_uuid(util &ul,const int& len) { for(int i=0;i<len;i++) { ul.get_uuid(); } } std::string util::get_str_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid,uuid_value); return std::string(uuid_value); } char *util::get_uuid() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } //main.cpp #include <unistd.h> #include <stdio.h> #include <time.h> #include "model/util.h" void invoke_func_args_demo(const int &len) { util ul; ul.invoke_func_args(ul,len); } int main(int argc, char **argv) { invoke_func_args_demo(atoi(argv[1])); }
Compile
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread
//std::function<void(util &,const int &)>func //func(ul,len) void util::stdfunction_invoke_func(util &ul,const int &len) { stdfunction_pass_func(ul,len,&get_str_uuid2); stdfunction_pass_func(ul,len,&get_char_uuid); } void util::stdfunction_pass_func(util &ul,const int &len,std::function<void(util &,const int &)>func) { std::chrono::time_point<std::chrono::high_resolution_clock> start_time,end_time; start_time=std::chrono::high_resolution_clock::now(); func(ul,len); end_time=std::chrono::high_resolution_clock::now(); std::uint64_t mills_count=std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count(); std::cout<<"Len="<<",Total Time cost "<<mills_count <<" milliseconds!"<<",Per Million Time cost "<<mills_count/(len/1000000)<<" milliseconds!"<<std::endl<<std::endl; }
void util::stdfunction_invoke_func(util &ul,const int &len) { stdfunction_pass_func(ul,len,&get_char_uuid); } void util::stdfunction_pass_func(util &ul,const int &len,std::function<void(util &,const int &)>func) { std::chrono::time_point<std::chrono::high_resolution_clock> start_time,end_time; start_time=std::chrono::high_resolution_clock::now(); func(ul,len); end_time=std::chrono::high_resolution_clock::now(); std::uint64_t mills_count=std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count(); std::cout<<"Len="<<len<<",Total Time cost "<<mills_count <<" milliseconds!"<<",Per Million Time cost "<<mills_count/(len/1000000)<<" milliseconds!"<<std::endl<<std::endl; }
标签:function,std,argument,c++,util,ul,len,func,time From: https://www.cnblogs.com/Fred1987/p/17217253.html