Performancefrage: Iteratoren



  • Hab gerade mal getestet, wie VC++ (2005 Express) Vektoren mit Iteratoren umsetzt, und mir die Assemblerausgabe angeguckt (release, mit Optimierungen, kein secure_sonstwas_Brei).

    #include <vector>
    #include <iostream>
    using namespace std;
    int main() {
    
        int csize = 10000;
        int* c = (int*) malloc (csize*sizeof(int));
        int* cend = c + csize;
        int sc = 0;
        for( int* ip = c; ip < cend; ++ip )
            sc += *ip;
        printf("%d", sc);
        free(c);
    
        typedef vector<int> TV;
        TV cpp(10000);
        int scpp(0);
        for( TV::const_iterator it( cpp.begin() ); it != cpp.end(); ++it )
            scpp += *it;
        cout << scpp << endl;
    
        return 0;
    }
    
    ; Line 10
        cmp    esi, edx
        jae    SHORT $LN4@main
        npad    4
    $LL6@main:
    ; Line 11
        add    ecx, DWORD PTR [eax]
        add    eax, 4
        cmp    eax, edx
        jb    SHORT $LL6@main
    $LN4@main:
    
    ; Line 18
        mov    esi, DWORD PTR _cpp$[esp+52]
        mov    edx, DWORD PTR _cpp$[esp+56]
        xor    ecx, ecx
        cmp    esi, edx
        mov    eax, esi
        je    SHORT $LN1@main
        npad    4
    $LL24@main:
    ; Line 19
        add    ecx, DWORD PTR [eax]
        add    eax, 4
        cmp    eax, edx
        jne    SHORT $LL24@main
    $LN1@main:
    

    Zumindest für die Schleife (ohne Initialisierung) ist hier komplett das selbe bei rausgekommen (Line 11 und Line 19).
    Wie das bei komplexeren Geschichten aussieht, weiß ich nicht. Ich bin aber oft überrascht, was Compiler so für Optimierungen hinbekommen.
    Zur Streamfrage kann ich leider nix beitragen.


  • Mod

    Dobi schrieb:

    Wie das bei komplexeren Geschichten aussieht, weiß ich nicht. Ich bin aber oft überrascht, was Compiler so für Optimierungen hinbekommen.

    Abgesehen davon dass Compiler dabei wirklich gut sind, gibt's da auch nicht mehr viel zu tun. Wie schon gesagt, wenn man keine Laufzeitprüfungen mehr hat ist ein vector-Iterator effektiv ein Pointer mit Syntaxzucker. Und von Syntaxzucker bleibt beim Compileren nichts übrig.



  • vector<T>::const_iterator könnte bei Dobis Test auch ein Alias für T const* gewesen sein. Das müsste man mal nachprüfen. In dem Fall wäre es nämlich gar keine besondere Optimierungsleistung. 🙂


Anmelden zum Antworten