performance - Allocate and free memory VS. allocate first, process only for each time -


i'm trying write robot hardware's serial communication part. , have desktop pc motherboard, no memory restrictions (8gb ram, i3 cpu etc.) program communicates microcontroller via usb(fullspeed) or serial 115200 baudrate. i'm confused problem's smallness. have 20 30 methods they're using communication function.

which 1 more effective processing fast? 1 instance of function running @ same time.

  1. define first, use everytime;

    ... private:     struct timespec ctv1, ctv2;     double time_diff;     int serial_write_ret;     int ret_val; ... int mserial::genandsend_setint32command()  {     gensum ( stm_buf_t );     sem_wait ( &serial_mutex );     // somefunctions();     sem_post ( &serial_mutex );     return ret_val; } 
  2. or allocate , deallocate everytime;

    int mserial::genandsend_setint32command() {     gensum ( stm_buf_t );     struct timespec ctv1, ctv2;     double time_diff = .0;     int serial_write_ret;     int ret_val = timeout_error_in_serial;      sem_wait ( &serial_mutex );     // somefunction();     sem_post ( &serial_mutex );     return ret_val; } 

is difference important?

my laziness...

this result of period of 80 * 60 * 60 (hz x seconds x minutes):

process only:: mean: 0.00356445 in seconds alloc dealloc:: mean: 0.0743379 in seconds 

here code , redundant outputs:

allocate - deallocate every time

class allocevery { public:      int dosomething()     {         double pi, gold, ogh;         std::string den_rit, jobs, bill;         char c_str[64];          pi   = 3.1415926535;         gold = 1.6180339887;         ogh  = 0.0000000033;         ogh += pi;         ogh += gold;         jobs = "being richest man in cemetery doesn't matter me. going bed @ night saying we've done wonderful, that's matters me.";         bill = "your unhappy customers greatest source of learning.";         den_rit = "unix simple operating system, have genius understand simplicity.";     } }; 

process only:

class processonly { public:      double pi, gold, ogh;     std::string den_rit, jobs, bill;     char c_str[64];      int dosomething()     {         pi   = 3.1415926535;         gold = 1.6180339887;         ogh  = 0.0000000033;         ogh += pi;         ogh += gold;         jobs = "being richest man in cemetery doesn't matter me. going bed @ night saying we've done wonderful, that's matters me.";         bill = "your unhappy customers greatest source of learning.";         den_rit = "unix simple operating system, have genius understand simplicity.";     } }; 

and main

int main ( int argc, char **argv ) {     int max = 80 * 60 * 5; // rate * seconds * minutes     struct timespec time1, time2;     double time_diff = .0;     allocevery obj; /// processonly obj;      clock_gettime ( clock_monotonic, &time1 );      (int = 0; < max; i++)     {         obj.dosomething();     }      clock_gettime ( clock_monotonic, &time2 );      time_diff = time2.tv_sec - time1.tv_sec + ( time2.tv_nsec - time1.tv_nsec ) / billion;      std::cout << "process only:: elapsed time: " << time_diff << std::endl;  return 0; } 

and outputs different runs:

ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.075384 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0741677 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.074426 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0740817 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0734898 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0747045 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0727975 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0772903 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./a.out ------------> alloc - dealloc everytime:: elapsed time: 0.0726992 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00806864 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00727956 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00202144 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00195636 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00203696 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00387936 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00276425 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00200299 ogh@ubuntu:~/c_denemeler/tryspeed_cpp$ ./p.out ------------>  process only:: elapsed time: 0.00207049 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -