0

I'm trying to write robot hardware's serial communication part. And I have a desktop pc motherboard, no memory restrictions (8GB RAM, i3 cpu etc.) for that program which communicates with microcontroller via USB(Fullspeed) or Serial with 115200 baudrate. I'm confused about my problem's smallness. I have a 20 to 30 methods which they're using this communication function.

Which one is more effective for processing fast? Only one instance of this function is running at the 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 and 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 that difference really important?

Orhan G. Hafif
  • 379
  • 5
  • 21
  • 1
    In this case, the `define first, use everytime` is better. But the difference is so small, that you will not notice it. – GMichael Aug 24 '16 at 09:30
  • Thank You! My communication speed doesn't reach these levels but, Doesn't it matter even if the speed will reach amount to 480,000,000 baud (USB 2.0)? – Orhan G. Hafif Aug 24 '16 at 09:45
  • 1
    The real difference is the initialization of the local variables inside `genAndSend_setInt32Command`. It should be not more than several ticks. You can also measure it if you like. – GMichael Aug 24 '16 at 09:54

1 Answers1

0

My laziness...

This is the result of period of 80 * 60 * 5 (Hz x Seconds x Minutes):

Process only:: mean: 0.00356445 in seconds
Alloc Dealloc:: mean: 0.0743379 in seconds

Here is the code and the 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 the richest man in the cemetery doesn't matter to me. Going to bed at night saying we've done something wonderful, that's what matters to me.";
        bill = "Your most unhappy customers are your greatest source of learning.";
        den_rit = "UNIX is basically a simple operating system, but you have to be a genius to understand the 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 the richest man in the cemetery doesn't matter to me. Going to bed at night saying we've done something wonderful, that's what matters to me.";
        bill = "Your most unhappy customers are your greatest source of learning.";
        den_rit = "UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.";
    }
};

And the 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 );

    for (int i = 0; i < 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 from different runnings:

ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.075384
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0741677
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.074426
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0740817
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0734898
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0747045
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0727975
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0772903
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./a.out
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0726992
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00806864
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00727956
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00202144
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00195636
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00203696
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00387936
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00276425
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00200299
ogh@ubuntu:~/C_Cpp_Examples/tryspeed_cpp$ ./p.out
------------>  Process only:: Elapsed time: 0.00207049
Orhan G. Hafif
  • 379
  • 5
  • 21