青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Zero Lee的專欄

Template and Inheritance

As Meyers noted in Item 24 of Effective C++,the inability to inline a virtual function is its biggest performace penalty.
Virtual functions seems to inflict a performace cost in several ways:
[1] The vptr must be initialized in the constructor.
[2] A virtual function is invoked via pointer indirection. We must fetch the pointer to the function table and then access the correct function offset.
[3] Inlining is a compile-time decision. The compiler cannot inline virtual functions whose resolution takes place at run-time.
The true cost of virtual functions then boils down to the third item only.

-------------------------------------------------------------------------------------------
Virtual function calls that can be resolved only at rum-time will inhibit inling. At times, that may pose a performace problem that we must solve. Dynamic binding of?a function call is a consequence of inheritance. One way to eliminate dyanamic binding is to replace inheritance with a template-based design. Templates are more performance-friendly in the sense that they push the resolution step from run-time to compile-time. Compile-time, as far as we are concerned, is free.

The desing space for inheritance and templates has some overlap. We will discuss one such example.

Suppose you wanted to develop a thread-safe string class that may be manipulated safely by concurrent threads in a Win32 environment. In that environment you have a choice of multiple synchronization schemes such ascriticalsection, mutex, and semanphores, just to name a few. You would like your thread-safe string to offer the flexibility to use any of those schemes, and at different times you may have a reason to prefer one scheme over another. Inheritance would be a reasonable choice to capture the commonality among synchronization mechanisms.

The Locker abstract base class will declare the common interface:

?1 class ?Locker
?2 {
?3 public :
?4 ????Locker()? {?}
?5 ???? virtual ? ~ Locker()? {?}
?6 ???? virtual ? void ? lock ()? = ? 0 ;
?7 ???? virtual ? void ?unlock()? = ? 0 ;
?8 }
;
?9
10 class ?CriticalSectionLock?:? public ?Locker
11 {?
12
13 }
;
14 class ?MutexLock?:? public ?Locker
15 {
16 ?
17 }
;
Because you prefer not to re-invent the wheel, you made the choice to derive the thread-safe string from the existing standard string. The remaining design choices are:

[1] Hard coding. You could derive three distinct classes from string::CriticalSectionString, MutexString, and SemaphoreString, each class implementing its implied synchronization mechanism.
[2] Inheritance. You could derive a single ThreadSafeString class that contains a pointer to a Locker object. Use polynorphism to select the particular synchronization mechanism at run-time.
[3] Templates. Create a template-based string class parameterized by the Locker type.
////////////////////////////////////////////////////////////////////////////////////////////
Here we only talk about the Template implementation.

The templates-based design combines the best of both worlds-reuse and efficiency. The ThreadSafeString is implemented as a?template parameterized by the Locker template argument:
?1template?<class?LOCKER>
?2class?ThreadSafeString?:?public?string
?3{
?4public:
?5???ThreadSafeString(const?char*?s)?
?6???:?string(s)?{?}
?7???
?8???int?length();
?9private:
10???LOCKER?lock;
11}
;
12
The length method implementation is similar to the previous ones:
?1template?<class?LOCKER>
?2inline
?3int?ThreadSafeString<LOCKER>?::?length()
?4{
?5??lock.lock();
?6??int?len?=?string::length();
?7??lock.unlock();
?8
?9??return?len;
10}
If you want critical section protection, you will instantiate the template with a CriticalSectionLock:
{
?? ThreadSafeString<CriticalSectionLock> csString = "Hello";
?? ...
}
or you may go with a mutex:
{
?? ThreadSafeString<MutexLock> mtxString = "Hello";
?? ...
}

This design also provides a relief from the virtual function calls to lock() and unlock(). The declaration of a ThreadSafeString selects a particular type of synchronization upon template instantiation time. Just like hard coding, this enables the compiler to resolve the virtual calls and inline them.

As you?can see, templates can make a positive performace contribution by pushing computations out of the excution-time and into compile-time, enabling inling in the process.

posted on 2006-11-13 13:37 Zero Lee 閱讀(290) 評論(0)  編輯 收藏 引用 所屬分類: C++ Performance

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久色在线观看| 久久国产精品毛片| 亚洲国产精品va在线看黑人| 久久综合色播五月| 亚洲精品乱码久久久久久蜜桃91| 亚洲国产精品999| 欧美欧美午夜aⅴ在线观看| 日韩视频在线一区| 一区二区三区免费看| 国产精品爽黄69| 久久香蕉国产线看观看av| 久久久久国内| 一区二区久久久久| 午夜精品久久久久久久久久久久久| 一区免费视频| 99精品热视频| 韩国av一区二区| 亚洲精品日本| 激情久久婷婷| 一个色综合导航| 激情欧美一区二区三区| 亚洲毛片在线观看.| 国产一区视频观看| 亚洲乱码国产乱码精品精可以看| 国产美女精品视频免费观看| 欧美激情视频一区二区三区免费| 国产精品v欧美精品∨日韩| 久久在线视频在线| 国产精品swag| 最新日韩欧美| 一区精品在线| 亚洲欧美激情四射在线日| 91久久国产综合久久91精品网站| 亚洲视频图片小说| 亚洲精品免费一二三区| 欧美一区二区视频免费观看| 中文在线不卡| 欧美v日韩v国产v| 久久久www成人免费无遮挡大片| 欧美—级a级欧美特级ar全黄| 久久久xxx| 国产精品日韩欧美一区二区三区 | 国产精品免费一区二区三区观看| 免费一级欧美片在线观看| 国产精品少妇自拍| 亚洲伦理在线免费看| 亚洲人成在线免费观看| 久久精品99无色码中文字幕| 性欧美8khd高清极品| 欧美日韩免费在线观看| 亚洲高清久久久| 亚洲第一精品在线| 欧美影院在线| 久久精品日产第一区二区| 国产精品三区www17con| 中文在线一区| 亚洲欧美中文字幕| 国产精品海角社区在线观看| 日韩视频在线一区二区| 日韩视频免费| 欧美日韩国产va另类| 91久久精品美女| 日韩视频一区二区三区| 欧美福利精品| 日韩视频免费观看| 国产精品99久久久久久人| 欧美日韩成人一区| 在线视频亚洲| 午夜亚洲视频| 国产亚洲欧美日韩日本| 香蕉久久国产| 久久一区二区三区四区| 黄网站免费久久| 欧美成年人视频| 日韩亚洲不卡在线| 午夜一级在线看亚洲| 国产一区二区在线免费观看 | 亚洲人体1000| 午夜精品久久99蜜桃的功能介绍| 国产精品日韩在线| 久久精品国产第一区二区三区| 女同性一区二区三区人了人一| 亚洲欧洲在线免费| 国产精品福利av| 久久精品午夜| 亚洲欧洲精品成人久久奇米网| 亚洲一区二区三区国产| 国产日产欧产精品推荐色| 久久夜色精品国产亚洲aⅴ| 亚洲国内精品| 欧美在线一级va免费观看| 在线播放一区| 欧美午夜不卡在线观看免费 | 一区二区三区亚洲| 欧美精品久久一区二区| 亚洲欧美综合另类中字| 欧美激情国产精品| 午夜精品久久久久久久久久久久| 影音欧美亚洲| 国产精品青草综合久久久久99| 久久一区二区三区四区五区| 亚洲美女av黄| 欧美jizzhd精品欧美喷水| 亚洲综合不卡| 亚洲日韩视频| 国产婷婷色综合av蜜臀av| 免费观看成人| 香蕉久久夜色精品国产| 91久久久久| 蜜桃av一区二区| 欧美亚洲自偷自偷| 9色国产精品| 亚洲国产精品久久久久久女王| 国产精品日本精品| 欧美日韩国产免费| 老司机午夜精品视频| 性欧美xxxx大乳国产app| 日韩视频国产视频| 亚洲电影欧美电影有声小说| 久久久国际精品| 午夜精品久久久久| 一区二区三区www| 亚洲国产精品成人一区二区| 国产一区二区激情| 国产精品一区二区久久精品| 欧美日韩免费观看一区三区 | 欧美国产一区二区三区激情无套| 久久99伊人| 欧美一区二区在线视频| 亚洲免费影视| 亚洲一区二区四区| 一本色道久久88综合日韩精品| 亚洲日本精品国产第一区| 欧美韩日高清| 亚洲国产日韩美| 亚洲国产经典视频| 亚洲国产精品尤物yw在线观看| 欧美成人国产| 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲图色在线| 亚洲综合第一页| 亚洲专区欧美专区| 亚洲四色影视在线观看| aa级大片欧美三级| 一区二区三区成人| 亚洲天天影视| 欧美一区二区三区免费视| 欧美一区二区三区啪啪| 久久国产精品色婷婷| 久久国产成人| 免费观看日韩av| 欧美日韩国产限制| 国产精品国码视频| 国产欧美一区二区三区在线老狼 | 亚洲精品久久久久久久久久久| 亚洲欧洲一区二区三区| 夜夜嗨av一区二区三区免费区| 一区二区三区日韩欧美| 亚洲在线黄色| 久久噜噜噜精品国产亚洲综合| 欧美a级片一区| 国产精品成人aaaaa网站| 国产精品麻豆成人av电影艾秋| 国产日韩欧美另类| 在线看欧美视频| 国产精品99久久久久久久女警| 欧美一级播放| 欧美激情1区| 亚洲一二三区精品| 久久久久久久网| 欧美日韩人人澡狠狠躁视频| 国产欧美日韩一区二区三区在线 | 久久久精品国产免费观看同学| 欧美成人一品| 亚洲一本视频| 女女同性女同一区二区三区91| 欧美日韩亚洲一区二区三区四区| 国产主播一区二区三区四区| 亚洲精品极品| 久久激情久久| 夜夜嗨av色综合久久久综合网| 欧美在线网址| 国产精品久久久久国产精品日日| 在线精品一区二区| 欧美一二三视频| 亚洲精品国产精品乱码不99按摩 | 亚洲女性喷水在线观看一区| 欧美xart系列在线观看| 国产人成精品一区二区三| 亚洲精品日韩综合观看成人91| 欧美在线免费视频| 艳女tv在线观看国产一区| 久久先锋资源| 国产中文一区| 欧美亚洲一级| 亚洲一区二区不卡免费| 欧美激情一区二区三区蜜桃视频 | 日韩性生活视频| 蜜臀久久99精品久久久久久9| 亚洲一区二区三区免费视频|