0

我正在尝试编写机器人硬件的串行通信部分。我有一个桌面电脑主板,该程序没有内存限制(8GB RAM, i3 cpu etc.),该程序通过USB (Fullspeed)或串行波特率与115200波特率进行通信。 我很困惑我的问题很小。我有一个20到30个他们正在使用这种通信功能的方法。分配和释放内存VS.首先分配,每次只处理

哪一种处理速度更快?此功能只有一个实例正在同时运行。

  1. 先定义,每次使用;

    ... 
    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. 或者每次分配和释放;

    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; 
    } 
    

那是差真的很重要?

+1

在这种情况下,'限定第一,使用everytime'更好。但差别非常小,你不会注意到它。 – GMichael

+0

谢谢!我的通信速度没有达到这些水平,但是,即使速度将达到480,000,000波特(USB 2.0),这无关紧要吗? –

+1

真正的区别在于'genAndSend_setInt32Command'内的局部变量的初始化。它不应该超过几个蜱。如果你喜欢,你也可以测量它。 – GMichael

回答

0

我的懒惰...

这是80 * 60 * 5(赫兹X秒X分钟)周期的结果:

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

下面是代码和冗余输出:

分配 - 每次释放

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."; 
    } 
}; 
只有个

过程:

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."; 
    } 
}; 

和主

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; 
} 

并输出来自不同运行:

[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.075384 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0741677 
[email protected]tu:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.074426 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0740817 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0734898 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0747045 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0727975 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0772903 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./a.out 
------------> Alloc - Dealloc Everytime:: Elapsed time: 0.0726992 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00806864 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00727956 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00202144 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00195636 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00203696 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00387936 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00276425 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00200299 
[email protected]:~/C_denemeler/tryspeed_cpp$ ./p.out 
------------> Process only:: Elapsed time: 0.00207049