בוא ניתן למספרים לדבר
לא אהבתי את שיטת שלוש השניות. אני מעדיף לעשות את הפעולה X פעמים ולבדוק זמנים - אז עשיתי את זה. קודם כל, הנה התוצאות (בשבילך התאמתי את זה לשלוש שניות פחות או יותר) - מדובר, אגב, ב - 1,100,000 פעולות.
Results: string dup = 2812ms, char dup = 1125ms. Results: string dup = 2750ms, char dup = 1078ms. Results: string dup = 2750ms, char dup = 1079ms. Results: string dup = 2843ms, char dup = 1110ms. Results: string dup = 2812ms, char dup = 1125ms. Results: string dup = 2844ms, char dup = 1109ms. Results: string dup = 2813ms, char dup = 1094ms. Results: string dup = 2828ms, char dup = 1109ms. Results: string dup = 2859ms, char dup = 1110ms. Results: string dup = 2797ms, char dup = 1093ms.
כלומר, פי שלוש לטובת char. עברתי ל-RELEASE, והתוצאות השתפרו פלאים, אבל עדיין יש בין 10% ל-20% לטובת ה-char:
Results: string dup = 547ms, char dup = 437ms. Results: string dup = 531ms, char dup = 438ms. Results: string dup = 531ms, char dup = 438ms. Results: string dup = 515ms, char dup = 453ms. Results: string dup = 516ms, char dup = 438ms. Results: string dup = 516ms, char dup = 437ms. Results: string dup = 532ms, char dup = 437ms. Results: string dup = 531ms, char dup = 438ms. Results: string dup = 531ms, char dup = 422ms. Results: string dup = 515ms, char dup = 453ms.
והתוכנית כולה (מבוססת על שלך):
#include <windows.h> #include <string> #include <iostream> using namespace std; std::string string_dup(const char* s); char* char_dup(const char* s); int main() { unsigned int num_of_chars = 1100000; unsigned int nStr, nChar; int i,j; for (j = 0; j < 10; j++) { nStr = GetTickCount(); for (i = 0; i < num_of_chars; i++) { string s = string_dup("Just a String"); } nStr = GetTickCount() - nStr; nChar = GetTickCount(); for (i = 0; i < num_of_chars; i++) { char* p = char_dup("Just a String"); delete []p; } nChar = GetTickCount() - nChar; cout << "Results: " << "string dup = " << nStr << "ms, char dup = " << nChar << "ms." << endl; } return 0; } //----------------------------------------------------------------------------- std::string string_dup(const char* s) { return std::string(s); } char* char_dup(const char* s) { int len = strlen(s) + 1; char* p = new char[len]; memcpy(p, s, len); return p; }
I REST MY CASE - כמו שאמרתי בהתחלה, יש הבדלי מהירות בין C לבין ++C. הם קיימים בתיאוריה ובפרקטיקה גם יחד. בספר (המומלץ מאוד מאוד) של Bulka ושל Mayhew, הנקרא ++Efficient C, הם אומרים ככה -
We concede that in general, when comparing a C program to a C++ version of what appears to be the same thing, the C program is generally faster. However, we also claim that the apparent similarity of the two progams typically is based on their data handling functionality, not their correctness, robustness, or ease of maintenance.
כלומר, כמו שאמרתי לאורך כל הדרך, תוכנה ב-C תהיה יותר מהירה באופן עקרוני, אבל היתרונות של ++C יותר ממכסים על החסרון היחסי הזה, כיוון שב-99% מהמקרים (אם אתה יודע מה אתה עושה), בסופו של דבר ++C מספקת לך כלים שכתיבה מחודשת שלהם ב-C תגרום לתוכנית לרוץ באיטיות גדולה יותר. למשל, אם היינו מכניסים לתמונה גם וקטור שגדל עם הזמן, הגרסה של C הייתה איטית יותר מהגרסה של ++C, בגלל ש-STL עושה עבודה מצוינת.