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

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>
            欧美专区一区二区三区| 欧美激情导航| 亚洲欧美日本国产有色| 免费在线看成人av| 午夜精品久久久久影视| 久久精品中文字幕一区| 欧美粗暴jizz性欧美20| 亚洲美女免费视频| 性久久久久久久久| 欧美日本一区二区三区| 国产精品视频一区二区高潮| 国产一级揄自揄精品视频| 亚洲国产三级网| 午夜精品一区二区三区四区| 亚洲一区二区三区在线观看视频 | 亚洲视频碰碰| 久久国产精品黑丝| 久久精品二区亚洲w码| 欧美高清视频在线播放| 一区二区三区高清在线| 美女视频网站黄色亚洲| 欧美精品一区二区三区久久久竹菊 | 亚洲一区视频| 韩日精品在线| 欧美亚洲一区二区三区| 一本久久a久久精品亚洲| 久久久亚洲国产天美传媒修理工| 欧美精品久久99| 亚洲福利国产精品| 久久久久国产精品一区| 另类图片综合电影| 韩国一区二区三区在线观看| 亚洲国产精品尤物yw在线观看| 亚洲日本成人| 亚洲性夜色噜噜噜7777| 亚洲国产精品ⅴa在线观看| 欧美色图一区二区三区| 亚洲欧洲日韩女同| 亚洲午夜精品久久| 欧美性大战久久久久久久| 亚洲精品国精品久久99热| 制服丝袜激情欧洲亚洲| 国产精品成人播放| 亚洲男人第一网站| 中国女人久久久| 在线国产欧美| 欧美成人一区二区三区在线观看| 欧美日韩中文字幕| 欧美va天堂va视频va在线| 国产精品久久久久久久久久免费 | 美女任你摸久久| 狠狠综合久久| 亚洲午夜在线观看视频在线| 亚洲精品资源| 999亚洲国产精| 亚洲欧洲视频在线| 久久九九精品| 亚洲激情成人在线| 欧美在线地址| 最新亚洲激情| 噜噜噜91成人网| 久久精品视频免费播放| 久久夜色精品国产| 亚洲美女中文字幕| 欧美va天堂| 亚洲欧美网站| 国产精品久久久久av| 亚洲人在线视频| 欧美日韩国产高清| 午夜精品偷拍| 国产精品久久久久久久久免费| 亚洲人成在线播放| 日韩一区二区高清| 欧美在线地址| 麻豆91精品91久久久的内涵| 欧美电影在线观看完整版| 亚洲午夜一区二区三区| 欧美日韩一区二区三区在线看 | 亚洲欧美日韩久久精品| 欧美无乱码久久久免费午夜一区| 亚洲精品国产拍免费91在线| 日韩午夜免费视频| 欧美日韩在线不卡| 一区二区日韩免费看| 亚洲综合视频网| 麻豆成人精品| 亚洲国产精品一区二区尤物区 | 国产欧美日韩精品a在线观看| 免费久久久一本精品久久区| 激情成人综合网| 亚洲先锋成人| 久久天堂av综合合色| 国产精品久久久对白| 先锋亚洲精品| 欧美激情综合| 黄色av成人| 欧美成人国产va精品日本一级| 欧美激情一区二区三区在线视频观看 | 欧美日本高清一区| 亚洲午夜精品一区二区| 久久久青草青青国产亚洲免观| 国产精品国产三级国产aⅴ入口 | 在线一区二区视频| 欧美主播一区二区三区| 国产综合色产| 午夜精品久久久久久久久久久久 | 在线一区二区三区四区五区| 国产精品免费观看在线| 久久嫩草精品久久久久| 一区二区日本视频| 欧美chengren| 午夜视频一区| 亚洲美女网站| 国产一区二区三区电影在线观看| 免费观看久久久4p| 亚洲欧美日韩精品久久奇米色影视| 久久综合久久综合这里只有精品 | 国产日产欧美a一级在线| 欧美a级一区| 亚洲欧美激情一区| 亚洲精品一区二区三区不| 亚洲日韩中文字幕在线播放| 国产欧美日韩亚州综合| 欧美日韩国产成人在线91| 久久人人97超碰精品888| 亚洲一区二区在线看| 99re66热这里只有精品4| 欧美va天堂| 久久久一区二区| 先锋影音国产一区| 亚洲无线视频| 亚洲精品一区二区三区福利 | 久久精品国产第一区二区三区最新章节| 狠狠久久婷婷| 国产精品日韩欧美一区二区| 欧美精品在线一区| 欧美jizz19hd性欧美| 久久国产精彩视频| 欧美一区在线看| 亚洲欧美另类在线| 亚洲一区二区不卡免费| 亚洲最新视频在线| 亚洲在线视频观看| 亚洲毛片在线观看| 亚洲乱码国产乱码精品精天堂| 精品不卡一区二区三区| 国色天香一区二区| 国产一区二区三区久久悠悠色av | 美女脱光内衣内裤视频久久影院| 亚洲欧美日韩精品久久奇米色影视| 夜夜嗨av一区二区三区免费区| 亚洲国内自拍| 99精品国产高清一区二区| 亚洲美女一区| 亚洲一区二区影院| 午夜久久影院| 久久国产精品久久久久久电车| 欧美一区二区视频观看视频| 欧美激情一区二区| 亚洲第一区在线| 欧美在线观看一二区| 亚洲精品免费在线观看| 亚洲精品一区二区三区四区高清| 亚洲国产福利在线| 日韩一区二区精品葵司在线| 99国产精品国产精品久久| 在线视频日韩| 性伦欧美刺激片在线观看| 久久av在线看| 久久伊人一区二区| 亚洲国产综合视频在线观看| 亚洲免费精彩视频| 性欧美xxxx大乳国产app| 久久亚洲精品网站| 欧美精品亚洲| 国产精品视频久久| 亚洲激情不卡| 午夜精品一区二区三区在线播放| 久久久久久久久久久久久女国产乱 | 亚洲乱码国产乱码精品精天堂 | 韩国一区电影| 亚洲精品自在在线观看| 亚洲免费视频网站| 免费看精品久久片| 亚洲最新在线视频| 久久久久久香蕉网| 国产精品男人爽免费视频1| 国外成人在线视频| 一区二区不卡在线视频 午夜欧美不卡在 | 国产女主播在线一区二区| 国产综合av| 一本高清dvd不卡在线观看| 久久精品亚洲一区二区| 亚洲国产精品精华液2区45| 亚洲综合电影| 欧美精品高清视频| 影音先锋亚洲一区| 激情综合电影网| 亚洲男同1069视频| 亚洲高清在线观看一区|