Geschwindigkeitsunterschied zwischen C-String und std::string
-
Danke, das ging ja schnell!
Mich wunderte das nur, da ja die C-String Funktionen, wie strcpy, strcmp usw., ja nicht direkt ein Teil von C++ sind, sondern halt C. Oder bin ich da falsch informiert?
Benötigt man eigentlich in C++ noch malloc, realloc und free, oder kann man alles bequem per new und delete regeln?
-
strcpy, ... und die ganzen String funtionen sind alle schon im std::string implementiert halt unter anderem namen (als operator Überladungen und den Copy Constructor).
malloc und so was sollte man nicht in C++ Verwenden, da es den Constructor einer Class nicht aufruft, sondern nur den Speicherplatz bereit stellt.
-
Als Input ein simples Beispiel, das den Geschwindigkeitsvorteil des C-strings bei einer Zuweisung zeigt:
// Zeitmessung #include <iostream> #include <ctime> // clock_t, clock(), ... #include <conio.h> #include <cmath> #include <string> #include <cstring> using namespace std; int main() { string cppstr = "Hallo"; char * cstr = "Hallo"; const int NMAX = 20; int max; cout << "Anzahl Schleifendurchlaeufe? "; cin >> max; clock_t t1,t2; double ts, tm=0; double a; cout << endl; for(int n=0; n<NMAX; ++n) { t1 = clock(); // Start for( int i=0; i<max;i++) { cppstr = "Hallo Welt"; //zu messende Aktion 1 } t2 = clock(); // Ende ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); // Zeit in Sekunden. cout << "Zeit Aktion 1: " << ts << " sec" << endl; tm += ts; // Das ist das Gleiche wie tm = tm + ts } tm/=NMAX; // Das ist das Gleiche wie tm = tm / NMAX cout << "Durchschnitts-Zeit bei Aktion 1: " << tm << " sec" << endl; tm=0; cout << endl; for(int n=0; n<NMAX; ++n) { t1 = clock(); for( int i=0; i<max;i++) { cstr = "Hallo Welt"; //zu messende Aktion 2 } t2 = clock(); ts = (t2-t1)/static_cast<float>(CLOCKS_PER_SEC); cout << "Zeit bei Aktion 2: " << ts << " sec" << endl; tm += ts; } tm/=NMAX; cout << "Durchschnitts-Zeit bei Aktion 2: " << tm << " sec" << endl; getch(); }
-
ROFL
-
ROFL = "rolling on the floor laughing"
This shorthand term is used in postings and online chat to show enthusiastic appreciation ...
-
Ist es wirklich erlaubt einen C-String auf diese Weise zu ändern:
cstr = "Hallo Welt";
Dass man einen C-String als Stringliteral mit
const char * Literal = "Hallo";
erzeugen darf weiß ich, aber man darf es danach doch nicht mehr ändern oder?
-
nein, erlaubt wäre es nur nicht, wenn du noch ein const nach dem * einfügen würdest.
der alte inhalt geht dabei natürlich verloren.
-
KPC schrieb:
Ist es wirklich erlaubt einen C-String auf diese Weise zu ändern:
Ändern? Du änderst den String ja nicht, du lässt nur den Zeiger auf etwas anderes zeigen...
-
Zeiger verbiegen geht eben immer noch am schnellsten.
-
Ok danke, ich dachte das alte Literal würde dann nicht mehr ordnungsgemäß gelöscht!
-
Literale werden überhaupt nicht gelöscht.
-
ähm...
clock () ist kein QueryPerformanceCounter, die ergebnisse dürften unbrauchbar sein.
-
-
Erhard Henkes schrieb:
ROFL = "rolling on the floor laughing"
This shorthand term is used in postings and online chat to show enthusiastic appreciation ...Danke, ohne dich wüsste ich immer noch nicht was ROFL heißt. SCNR