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

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 閱讀(296) 評論(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>
            亚洲高清在线观看| 在线亚洲欧美视频| 欧美一区二区在线| 国产亚洲欧美一区二区| 小黄鸭精品aⅴ导航网站入口 | 国产在线拍偷自揄拍精品| 午夜欧美大尺度福利影院在线看| 在线视频中文亚洲| 国产精品免费看| 欧美专区中文字幕| 久久综合婷婷| 在线视频一区观看| 一本色道久久综合| 国产欧美日韩在线观看| 六月丁香综合| 欧美久色视频| 欧美在线视频播放| 久久一二三国产| 在线视频日本亚洲性| 亚洲专区一二三| 亚洲二区精品| 一级日韩一区在线观看| 国产一区二区三区在线观看免费| 欧美aa国产视频| 欧美午夜精品久久久久久孕妇| 性欧美video另类hd性玩具| 久久久久久有精品国产| 中日韩在线视频| 久久久久综合网| 亚洲免费伊人电影在线观看av| 午夜精品一区二区三区在线| 亚洲国产免费看| 亚洲欧美一区二区原创| 亚洲精品欧美在线| 午夜在线精品偷拍| 一本色道久久综合狠狠躁篇的优点 | 一区二区三区四区五区精品视频 | 99精品视频免费全部在线| 国产日韩久久| 99pao成人国产永久免费视频| 国精品一区二区| 亚洲视频网站在线观看| 亚洲人成网站精品片在线观看 | 亚洲高清不卡av| 国产伦精品一区二区三区| 亚洲高清不卡在线| 国产主播精品在线| 亚洲性线免费观看视频成熟| 亚洲人成网在线播放| 久久久久久久一区| 久久精品国产2020观看福利| 欧美日韩综合精品| 亚洲清纯自拍| 91久久综合| 久久躁狠狠躁夜夜爽| 久久久91精品国产一区二区三区| 欧美视频免费在线观看| 亚洲日本免费电影| 亚洲精品午夜| 欧美激情精品久久久久久黑人| 久久综合精品一区| 韩国自拍一区| 久久精品国产精品亚洲| 久久久国产视频91| 国产日产精品一区二区三区四区的观看方式 | 激情小说另类小说亚洲欧美 | 亚洲欧美日韩成人| 亚洲一区二区毛片| 欧美午夜精品| 亚洲一级黄色| 久久xxxx精品视频| 国产亚洲一本大道中文在线| 午夜精品www| 久久蜜臀精品av| 伊人久久大香线| 久久男女视频| 亚洲精品国产精品乱码不99| 一区二区高清在线| 欧美日韩在线播放三区四区| av成人国产| 欧美一级视频一区二区| 国产在线麻豆精品观看| 久久久久99| 亚洲国产美女| 亚洲一区二区三区777| 国产精品久久久亚洲一区| 亚洲欧美一级二级三级| 免播放器亚洲一区| 亚洲精品在线观看免费| 欧美色图天堂网| 欧美一区1区三区3区公司| 久久精品最新地址| 亚洲区中文字幕| 国产精品久久久久久久7电影 | 亚洲网站在线观看| 久久精品91久久香蕉加勒比| 精品99视频| 欧美日韩理论| 欧美在线免费观看亚洲| 亚洲国产婷婷香蕉久久久久久99 | 欧美在线看片a免费观看| 狠狠久久婷婷| 欧美日韩免费一区二区三区视频| 亚欧成人精品| 亚洲精品美女免费| 久久久久国内| 宅男噜噜噜66一区二区| 樱桃国产成人精品视频| 国产精品电影在线观看| 快she精品国产999| 亚洲一区二区视频在线观看| 欧美大片一区二区三区| 性久久久久久| 9色精品在线| 在线看国产日韩| 国产精品久久久久一区二区| 免费在线欧美黄色| 欧美一区二区三区免费观看| 亚洲伦理精品| 欧美激情一区二区三区在线视频观看| 午夜久久久久久| 一区二区三区免费网站| 亚洲福利在线看| 国产亚洲欧美另类一区二区三区| 欧美日韩国产丝袜另类| 免费成人高清| 久久精品一本久久99精品| 亚洲新中文字幕| 日韩视频不卡| 亚洲欧洲日产国产综合网| 噜噜噜躁狠狠躁狠狠精品视频| 西西人体一区二区| 亚洲在线国产日韩欧美| 亚洲免费久久| 亚洲精品日本| 91久久久在线| 亚洲毛片一区二区| 91久久精品国产91久久性色| 韩日成人在线| 激情久久中文字幕| 在线成人激情黄色| 在线观看免费视频综合| 在线激情影院一区| 激情欧美日韩| 狠狠色综合日日| 国外成人在线视频| 狠狠色综合播放一区二区| 国内精品写真在线观看| 韩日成人av| 亚洲福利视频网站| 亚洲精品乱码久久久久久日本蜜臀| 亚洲第一色在线| 亚洲欧洲另类| 亚洲深夜福利| 午夜视频精品| 久久久久久久综合| 欧美**人妖| 最新亚洲电影| 亚洲视频精选在线| 亚洲欧美日韩直播| 久久精品视频va| 欧美成人影音| 国产精品久久久久天堂| 国产一区二区福利| 亚洲国产一区二区三区在线播| 亚洲精品久久视频| 亚洲欧美在线另类| 久久久噜噜噜久噜久久| 亚洲第一黄色| 亚洲欧美成人一区二区三区| 欧美中文在线视频| 欧美激情第二页| 国产精品日韩精品欧美在线 | 欧美日韩高清在线观看| 国产精品中文在线| 亚洲第一网站| 亚洲欧美精品一区| 欧美11—12娇小xxxx| 亚洲美女色禁图| 欧美一区二区视频在线观看| 你懂的视频欧美| 国产精品综合| 亚洲欧洲在线免费| 欧美一区二区成人| 欧美二区在线| 亚洲欧美清纯在线制服| 欧美福利视频| 国内精品国语自产拍在线观看| 一二三区精品| 毛片一区二区| 亚洲女同精品视频| 欧美精品www在线观看| 国产小视频国产精品| 亚洲天堂男人| 欧美激情精品久久久久久黑人 | 欧美日韩视频在线第一区| 极品少妇一区二区三区精品视频| 一区二区三区产品免费精品久久75| 久久精品理论片| 亚洲一区二区免费|