~AnyClass() { #ifndef ANYCLASS_NO_OUTPUT std :: cout<< " AnyClass析构函数: << name<< ''\ n''; #endif --object_counter_prv; } }; #endif 然后我掀起了一个快速测试工具: #define ANYCLASS_NO_OUTPUT #include< time.h> #ifdef USESTRING #include" anyclass.hpp" #else #include" anyclass1.hpp" #endif #include< iostream> int main(){ const int num = 5000; AnyClass * x [num]; clock_t start = clock(); x [0] = new AnyClass(" X"); for(int i = 1; i< num; i ++) x [i] = new AnyClass(* x [i-1 ]); clock_t整体=时钟() - 开始; #ifdef USESTRING std :: cout<< ; 长度: << x [num-1] - > GetName()。length(); #else std :: cout<< 长度: << strlen(x [num-1] - > GetName()); #endif std :: cout<< " \ nTime:" <<总体/(双)CLOCKS_PER_SEC<< " seconds \ n" ;; 返回0; } 我使用VC ++ 7.1编译代码: cl / O2b2 / G7ry any.cpp anyclass.cpp 和: cl / DUSESTRING / O2b2 / G7ry any.cpp anyclass.cpp 并运行两个版本。在我的特定机器上,两个版本声称 在.312秒内运行。有可能(实际上可能),如果你经常运行它们,你至少偶尔会看到一个 的速度差异(在一个时钟刻度的顺序,我会 猜测)。通过大量的运行和仔细的统计分析,你甚至可能会发现它们之间存在统计上显着的差异 - 但是没有更多的测试,它很难甚至猜猜哪一个可能会赢得 - std :: string可能会更快,因为更慢。在任何情况下,我认为这或多或少都是无关紧要的:那里有 没有真正的空间可以解释使用std :: string的代码很多 更简单,使用原始char *的代码显然不是更快, 这是证明自己存在的必要条件。 我还会注意到使用原始指针会使异常安全性提高 更难以提供。 - 后来, 杰瑞。 宇宙是自己想象的虚构。 The name for that kind of code is "fragile." If you insist on writingsomething like this, a bare minimum of decency would be code somethinglike: static char cpy[] = "Copy of ";static size_t len = sizeof(cpy); memcpy(name, cpy, len-1);memcpy(name+len-1, original.name, length-=len); This way, you don''t have a "magic" number like 8 in the code, insteadusing a value whose derivation is obvious. Better still, if (forexample) somebody decided to translate the code to their choice oflanguages other than English, where "copy of" would almost certainlybe a different size, the size would automatically change with thechange in string length. IMO, all of this remains essentially pointless: given a halfwayreasonable implementation of the standard library, I''d expectstd::string to be right in the same general ballpark for speed asdoing this manually. To test this theory, I created a modified versionof your class using std::string: //anyclass.hpp #ifndef INCLUDE_ANYCLASS_HPP#define INCLUDE_ANYCLASS_HPP #ifndef ANYCLASS_NO_OUTPUT#include <iostream>#endif #include <string> class AnyClass{public:static unsigned const &object_counter; unsigned to_play_with; std::string const &GetName() const{return name;}private: static unsigned object_counter_prv;std::string name; public: AnyClass(std::string const &in_name, unsigned in_to_play_with =0): to_play_with(in_to_play_with), name(in_name){++object_counter_prv; #ifndef ANYCLASS_NO_OUTPUTstd::cout << " AnyClass Constructor for: " << name <<''\n'';#endif }AnyClass(AnyClass const &original) :to_play_with(original.to_play_with){name = std::string("Copy of ") + original.name;++object_counter_prv; #ifndef ANYCLASS_NO_OUTPUTstd::cout << "AnyClass Copy Constructor for: " << name <<''\n'';#endif} AnyClass& operator=(AnyClass const &other){//NB: There is no name change whatsoever to_play_with = other.to_play_with; #ifndef ANYCLASS_NO_OUTPUTstd::cout << " AnyClass Assignment Operator: " <<name << " = " << other.name << ''\n'';#endif return *this;} ~AnyClass(){#ifndef ANYCLASS_NO_OUTPUTstd::cout << " AnyClass Destructor for: " << name <<''\n'';#endif--object_counter_prv;}}; #endif and then I whipped together a quick test harness: #define ANYCLASS_NO_OUTPUT #include <time.h> #ifdef USESTRING#include "anyclass.hpp"#else#include "anyclass1.hpp"#endif #include <iostream>int main() { const int num = 5000; AnyClass *x[num]; clock_t start = clock();x[0] = new AnyClass("X");for (int i=1; i<num; i++)x[i] = new AnyClass(*x[i-1]);clock_t overall = clock()-start; #ifdef USESTRINGstd::cout << "length: " << x[num-1]->GetName().length();#elsestd::cout << "length: " << strlen(x[num-1]->GetName());#endif std::cout << "\nTime: " << overall/(double)CLOCKS_PER_SEC <<" seconds\n";return 0;} I compiled the code with VC++ 7.1 using: cl /O2b2 /G7ry any.cpp anyclass.cpp and: cl /DUSESTRING /O2b2 /G7ry any.cpp anyclass.cpp and ran both versions. On my particular machine, both versions claimedto run in .312 seconds. It''s possible (probable, in reality) that ifyou ran them often enough that you''d at least ocassionally see adifference in speed (on the order of a single clock tick worth, I''dguess). With a lot of runs and careful statistical analysis, you mighteven even find a statistically significant difference between them --but without more testing, it''s hard to even guess which one would belikely to win -- std::string might be about as likely to be faster asslower. In any case, I think that''s more or less irrelevant: there''sno real room for question that the code using std::string is a LOTsimpler, and the code using raw char *''s clearly is not a LOT faster,which is what would be needed to justify its own existence. I''d also note that using raw pointers makes exception safetyconsiderably more difficult to provide. --Later,Jerry. The universe is a figment of its own imagination. 这篇关于AnyClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-19 01:16