0

Ich versuche, den seriellen Kommunikationsteil der Roboter-Hardware zu schreiben. Und ich habe ein Desktop-PC-Motherboard, keine Speicherbeschränkungen (8GB RAM, i3 cpu etc.) für das Programm, das kommuniziert mit dem Mikrocontroller über USB (Fullspeed) oder Serial mit 115200 Baudrate. Ich bin verwirrt über die Kleinheit meines Problems. Ich habe eine 20 bis 30 Methoden, die diese Kommunikationsfunktion verwenden.Zuweisen und freien Speicher VS. zuerst zuweisen, nur für jedes mal verarbeiten

Welche ist effektiver für die schnelle Verarbeitung? Nur eine Instanz dieser Funktion wird gleichzeitig ausgeführt.

  1. Zuerst definieren, immer verwenden;

    ... 
    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. Oder allokieren und freigeben jedes Mal;

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

Ist dieser Unterschied wirklich wichtig?

+1

In diesem Fall ist die erste 'definieren, verwenden everytime' ist besser. Aber der Unterschied ist so klein, dass Sie es nicht bemerken werden. – GMichael

+0

Vielen Dank! Meine Kommunikationsgeschwindigkeit erreicht diese Werte nicht, aber spielt es keine Rolle, ob die Geschwindigkeit 480.000.000 Baud (USB 2.0) erreichen wird? –

+1

Der wirkliche Unterschied ist die Initialisierung der lokalen Variablen in 'genAndSend_setInt32Command'. Es sollte nicht mehr als mehrere Ticks sein. Sie können es auch messen, wenn Sie möchten. – GMichael

Antwort

0

Meine Faulheit ...

Dies ist das Ergebnis der Periode von 80 * 60 * 5(Hz x Sekunden x Minuten):

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

Hier ist der Code und die redundanten Ausgänge:

Zuweisen - jedes Mal zurücksetzen

nur

Prozess:

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

Und die Haupt

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

und Ausgänge von verschiedenen Läufen:

[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 
Verwandte Themen