本文介绍了令人惊讶的模板大小和速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我最近开始在C ++中使用模板,并发现它非常有用。 非常有用。然而,听到代码臃肿和各种问题的故事,我决定编写几个小程序来检查。我预计的是,使用模板时会有轻微的代码膨胀和一些速度提高。但是... 我写了一个基本列表容器(使用模板)和一个列表容器 (使用虚拟派生类)。我也试过std lib vector class。我运行了1000000次插入并访问了所有这些列表并获得了以下结果(使用微软编译器在windows上编译): 大小: 22,528 mytpl.exe 36,864 nonstd.exe 40,960 std.exe 第一个是我的模板列表,第二个我的非模板列表, 第三个是使用标准向量。 第一个惊喜是我的模板列表实际上是*更小* 比非模板版本!非常令人惊讶。但是,并不是所有的好消息都是好消息。然后我运行了一些计时测试。 时间: 875:运行std.exe 1484:运行nonstd.exe 1563:运行mytpl.exe 正如预期的那样,std向量是最快的。不过我的模板列表 类是*最慢* !!!这绝对不是预期的。 有没有人对正在发生的事情有任何意见? 欢呼, / CharlesHi,I have recently begun using templates in C++ and have found it to bequite useful. However, hearing stories of code bloat and assortedproblems I decided to write a couple of small programs to check. What Iexpected was that there would be minor code bloat and some speedimprovement when using templates. However...I wrote a basic list container (using templates), and a list container(using virtual derived classes). I also tried with the std lib vectorclass. I ran 1000000 inserts and accesses to all these lists and gotthe following results (compiling on windows with microsoft compiler):Size:22,528 mytpl.exe36,864 nonstd.exe40,960 std.exeThe first is my template list, the second my non-template list, and thethird is using the std vector.The first surprise was that my template list was actually *smaller*than the non-template version! Very surprising. However, it''s not allgood news. I then ran some timing tests.Time:875: running std.exe1484: running nonstd.exe1563: running mytpl.exeAs expected, the std vector is the fastest. However my template listclass is the *slowest*!!! This was definitly not expected.Does anyone have any inputs on what''s going on?cheers,/Charles推荐答案 不是。在_your_计算机上,如果没有任何 测量任何东西的能力,怎么能告诉我们有什么关于 _your_ program的性能呢?你在开玩笑吗? V - 请在通过电子邮件回复时删除资金'A' 我没有回复最热门的回复,请不要问Not really. How can anyone tell anything about the performance of_your_ program we cannot see, on _your_ computer, without anyability to measure anything? Are you kidding?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 不是。在_your_计算机上,如果没有任何 测量任何东西的能力,怎么能告诉我们有什么关于 _your_ program的性能呢?你在开玩笑吗? V - 请在通过电子邮件回复时删除资金'A' 我没有回复热门帖子的回复,请不要问Not really. How can anyone tell anything about the performance of_your_ program we cannot see, on _your_ computer, without anyability to measure anything? Are you kidding?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 嗯....好吧那是's好点子。我无法找到任何上传方式 我的代码。 但是,它很小,所以我只是粘贴它下面 -----------------------模板列表---------------- ------------ 模板< class T> class ListNode { public: ListNode(T * pVal){uVal = pVal;} T * uVal; ListNode< T> * uNext; ListNode< T> * uPrev; }; 模板< class T> 类MyTList { public: MyTList(){vFirst = NULL; } void插入(T * e){ ListNode< T * ln; ln = new ListNode< T( e); ln-> uNext = vFirst; ln-> uPrev = NULL; if(vFirst!= NULL) vFirst-> uPrev = ln; vFirst = ln; } ListNode< T * GetNodes (){return vFirst;} private: ListNode< T * vFirst; }; - --------------------------------模板列表结束 ------- --------------------- --------------------- ------------非模板列表 --------------------------- - class ListElem { public: virtual~ListElem(void){} ListElem * uNext; ListElem * uPrev; }; class MyList { public: MyList(){vFirst = NULL;} void Insert(ListElem * e){ e-> uNext = vFirst; e-> uPrev = NULL; if(vFirst!= NULL) vFirst-> uPrev = e; vFirst = e; } ListElem * GetNodes(){return vFirst; } 私人: ListElem * vFirst; }; --------- ---------------------非模板列表结束 ----------------- --------- 请注意,代码非常相似。我必须得到一个ListElem for 非模板类: class MyClassElem:public ListElem { public: MyClassElem(MyClass * c){uMyClass = c; } MyClass * uMyClass; }; 但是没有为模板版本做任何事情。 cpp测试代码基本上只运行两个循环(插入和 get-ting)。 欢呼, / CharlesHmmm.... well that''s a good point. I couldn''t find any way of uploadingmy code.However, it''s pretty small, so I''ll just paste it below----------------------- Template List ----------------------------template <class T>class ListNode{public:ListNode (T * pVal) {uVal = pVal;}T * uVal;ListNode<T>* uNext;ListNode<T>* uPrev;};template <class T>class MyTList{public:MyTList () { vFirst = NULL; }void Insert (T * e) {ListNode<T* ln;ln = new ListNode<T(e);ln->uNext = vFirst;ln->uPrev = NULL;if (vFirst != NULL)vFirst->uPrev = ln;vFirst = ln;}ListNode<T* GetNodes () { return vFirst;}private:ListNode<T* vFirst;};--------------------------------- Template List ends------------------------------------------------------------- Non Template List-----------------------------class ListElem{public:virtual ~ListElem (void) {}ListElem * uNext;ListElem * uPrev;};class MyList{public:MyList () { vFirst = NULL;}void Insert (ListElem * e) {e->uNext = vFirst;e->uPrev = NULL;if (vFirst != NULL)vFirst->uPrev = e;vFirst = e;}ListElem * GetNodes () { return vFirst; }private:ListElem * vFirst;};------------------------------ Non template list ends--------------------------Note that the code is *very* similar. I had to derive a ListElem forthe non-template class:class MyClassElem : public ListElem{public:MyClassElem (MyClass * c) { uMyClass = c; }MyClass * uMyClass;};But didn''t have to do anything for the template version.The cpp test code basically just runs two loops (inserting andget-ting).cheers,/Charles 不是真的。怎么能在_your_计算机上告诉我们无法看到 _your_程序的性能,没有任何测量能力的能力?你在开玩笑吗? V - 请在通过电子邮件回复时删除资金''A'我不回复顶部帖子回复,请不要问 Not really. How can anyone tell anything about the performance of_your_ program we cannot see, on _your_ computer, without anyability to measure anything? Are you kidding?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 嗯....好吧,这是一个好点。我无法找到任何上传我的代码的方式。Hmmm.... well that''s a good point. I couldn''t find any way ofuploading my code. 你的意思是什么?What do you mean? 最好全部使用它,尤其是看你如何使用 你的类和向量。It would be best to have all of it, especially to see how you useyour classes and the vector. 这是用词不当。你可能想把它称为''MyTPTRList'',因为 你没有存储T,但你存储了指向T的指针。 现在,你不知道你是如何填补它的。你是在免费商店创建 对象(使用''new'')?That''s a misnomer. You might want to call it ''MyTPTRList'', sinceyou''re not storing T, but you store pointers to T.Now, you don''t show how you actually fill it up. Are you creatingobjects in the free store (using ''new'')? 我从ListElem获取了你的代码,派生了DoubleListElem,这里是测试驱动程序的 : int main() { clock_t c0 = clock(); MyTList< doublemtld; for(int i = 0; i< 1000000; ++ i) mtld.Insert(new double(i)); clock_t c1 = clock(); MyList mld; for(int i = 0; i< 1000000; ++ i) mld.Insert(new DoubleListElem(new double(i))); clock_t c2 = clock(); std :: cout<< 模板: << c1-c0<< std :: endl; std :: cout<< Nontemplate: << c2-c1<< std :: endl; } 模板结果更快一点。现在,如果你想要真的把它与''std :: vector''的行为进行比较,你需要 (a)存储值,而不是指针, (b)找出更好的''插入'' 策略,可能没有''if''。 因为你的模板和非代码-template List是 相同,实际上应该基本上没有区别(并且 我的测试中没有)。你要完成的是什么? V - 请删除大写''A'' s通过电子邮件回复 我没有回复最热门的回复,请不要问I took your code, derived DoubleListElem from ListElem, and here isthe test driver:int main(){clock_t c0 = clock();MyTList<doublemtld;for (int i = 0; i < 1000000; ++i)mtld.Insert(new double(i));clock_t c1 = clock();MyList mld;for (int i = 0; i < 1000000; ++i)mld.Insert(new DoubleListElem(new double(i)));clock_t c2 = clock();std::cout << "Template: " << c1 - c0 << std::endl;std::cout << "Nontemplate: " << c2 - c1 << std::endl;}The template turned out a tiny bit faster. Now, if you want toreally compare it to the behaviour of ''std::vector'', you need to(a) store values, not pointers, (b) figure out better ''Insert''strategy, possibly without the ''if''.Since your code for both template and non-template List is thesame, virtually, there should be essentially no difference (andthere wasn''t in my test). What is it you''re trying to accomplish?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 这篇关于令人惊讶的模板大小和速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-15 15:19