0

J'essaie d'écrire la partie de communication série du robot. Et j'ai une carte mère PC de bureau, pas de restrictions de mémoire (8GB RAM, i3 cpu etc.) pour ce programme qui communique avec le microcontrôleur via USB (Fullspeed) ou série avec 115200 en baud. Je suis confus au sujet de la petitesse de mon problème. J'ai 20 à 30 méthodes qu'ils utilisent cette fonction de communication.Allouer et libérer de la mémoire VS. allouer en premier, traiter seulement pour chaque fois

Lequel est le plus efficace pour le traitement rapide? Une seule instance de cette fonction s'exécute en même temps.

  1. Définir en premier, utiliser à chaque fois;

    ... 
    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. Ou allouer et libérer chaque fois;

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

Est-ce que la différence est vraiment important?

+1

Dans ce cas, la 'définir d'abord, l'utilisation everytime' est mieux. Mais la différence est si petite que vous ne le remarquerez pas. – GMichael

+0

Merci! Ma vitesse de communication n'atteint pas ces niveaux, mais n'est-ce pas important même si la vitesse atteindra 480 000 000 bauds (USB 2.0)? –

+1

La vraie différence est l'initialisation des variables locales dans 'genAndSend_setInt32Command'. Il ne devrait pas y avoir plus de plusieurs ticks. Vous pouvez également le mesurer si vous le souhaitez. – GMichael

Répondre

0

Ma paresse ...

Ceci est le résultat de la période de 80 * 60 * 5(Hz x secondes x minutes):

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

Voici le code et les sorties redondantes:

Affecter - Délocaliser à chaque fois

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

processus seulement:

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

Et la principale

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

et sorties de différentes pistes:

[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]:~/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