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

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>
            亚洲欧洲99久久| 老司机精品导航| 在线视频亚洲一区| 欧美日韩国产一级| 亚洲一区二区三区免费观看 | 在线视频一区观看| 亚洲最新中文字幕| 国产裸体写真av一区二区| 久久蜜桃av一区精品变态类天堂| 久久久久久久999| 亚洲精品资源美女情侣酒店| 一本色道久久88综合日韩精品| 国产精品久久久999| 久久久久久欧美| 欧美成人午夜| 欧美亚洲免费| 免费观看成人| 香蕉久久国产| 欧美sm视频| 久久成人人人人精品欧| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲综合视频1区| 久久一区欧美| 午夜精品久久久久久久男人的天堂| 久久精品成人一区二区三区蜜臀| 亚洲美女av在线播放| 亚洲在线一区| 亚洲视频一区二区在线观看| 国产亚洲女人久久久久毛片| 欧美激情精品久久久久久| 国产精品久久久久久av下载红粉| 久久网站免费| 国产精品你懂的| 亚洲欧洲日本专区| 国产欧美精品久久| 99re成人精品视频| 亚洲国产乱码最新视频| 欧美亚洲尤物久久| 亚洲欧美精品| 欧美久色视频| 欧美国产成人在线| 狠狠干综合网| 香蕉国产精品偷在线观看不卡| 国产精品99久久久久久有的能看| 欧美xart系列高清| 国产精品一区三区| 一区二区三区久久| 亚洲视频日本| 欧美精品18| 亚洲国产欧美一区二区三区久久| 国产一区再线| 欧美在线视频日韩| 欧美中文在线观看国产| 国产精品视频第一区| 99精品热视频| 亚洲天堂av高清| 欧美日韩色一区| 亚洲乱码国产乱码精品精天堂| 亚洲欧洲综合| 欧美激情按摩| 久久激情五月激情| 国产欧美日韩免费| 亚洲免费在线| 久久精品视频免费| 国内在线观看一区二区三区| 欧美中文字幕在线视频| 久久精品国产91精品亚洲| 国产日韩在线视频| 久久久精品国产一区二区三区| 久久久综合网| 亚洲激情另类| 欧美精品一区二区在线播放| 亚洲精品视频一区| 亚洲欧美日韩专区| 国产婷婷色综合av蜜臀av | 欧美激情国产精品| 亚洲日本成人在线观看| 欧美精品v日韩精品v国产精品| 亚洲精品国产无天堂网2021| 亚洲香蕉网站| 国产亚洲欧美在线| 久久综合综合久久综合| 亚洲精品一区在线观看| 亚洲永久免费视频| 国内精品久久国产| 免费不卡中文字幕视频| 亚洲久久在线| 久久婷婷国产综合国色天香| 亚洲国产三级网| 国产精品免费一区二区三区在线观看| 欧美一区二区精品在线| 欧美国产亚洲视频| 亚洲欧美日韩国产综合| 精品不卡在线| 国产精品久久久久久久久久免费 | 亚洲欧美日韩一区在线观看| 另类春色校园亚洲| 中国日韩欧美久久久久久久久| 国产精品女人网站| 美女91精品| 羞羞色国产精品| 亚洲人成毛片在线播放| 欧美伊久线香蕉线新在线| 亚洲国产三级网| 国产日韩高清一区二区三区在线| 欧美福利一区二区三区| 午夜精品久久久久久久白皮肤| 亚洲韩日在线| 老牛嫩草一区二区三区日本| 亚洲色无码播放| 亚洲二区在线视频| 国产亚洲美州欧州综合国| 欧美日韩国产影片| 久久躁狠狠躁夜夜爽| 午夜精品久久久久| aa级大片欧美三级| 亚洲电影网站| 欧美a级理论片| 久久久久国产精品www| 亚洲天堂偷拍| 一区二区三区国产| 亚洲精品欧美| 在线视频国产日韩| 黄色一区二区在线| 国产三区二区一区久久| 国产精品二区三区四区| 欧美福利视频在线| 免费在线看成人av| 久久夜色精品亚洲噜噜国产mv | 欧美高清视频一二三区| 久久―日本道色综合久久| 欧美一区二区三区视频免费播放| 在线一区免费观看| 在线一区亚洲| 亚洲一区二区免费在线| 正在播放欧美视频| 亚洲一区二区精品在线| 国产精品99久久久久久白浆小说| 99热免费精品在线观看| 欧美日韩八区| 欧美日韩国产小视频在线观看| 欧美福利在线观看| 欧美极品在线播放| 欧美日韩免费在线视频| 欧美四级剧情无删版影片| 欧美视频1区| 国产精品亚洲激情| 国产一区91| 在线国产欧美| 亚洲精品在线一区二区| 一级成人国产| 欧美有码视频| 免费观看30秒视频久久| 亚洲福利国产精品| 99xxxx成人网| 香蕉精品999视频一区二区| 欧美在线视频一区二区三区| 久久久亚洲精品一区二区三区| 久久免费精品日本久久中文字幕| 美女日韩在线中文字幕| 欧美激情亚洲视频| 国产精品视频1区| 红桃视频国产一区| 日韩一区二区精品| 欧美在线播放高清精品| 欧美mv日韩mv亚洲| 日韩一区二区精品葵司在线| 亚洲欧美第一页| 麻豆国产精品va在线观看不卡| 欧美日韩精品在线播放| 国产精品亚洲网站| 亚洲日本成人网| 性色av香蕉一区二区| 欧美大胆人体视频| 亚洲一区二区三区午夜| 麻豆精品视频在线观看| 国产精品女人毛片| 亚洲精品久久| 久久激情综合网| 日韩一区二区免费高清| 久久爱www久久做| 欧美日韩亚洲一区在线观看| 国产视频在线观看一区 | 国产精品日产欧美久久久久| 亚洲国产aⅴ天堂久久| 午夜精品美女久久久久av福利| 欧美国产精品人人做人人爱| 国产精品99久久久久久久vr | 亚洲一区在线免费观看| 美女露胸一区二区三区| 国产日韩专区| 亚洲一区在线观看视频| 欧美激情网友自拍| 欧美亚洲综合久久| 欧美亚州一区二区三区| 亚洲人成毛片在线播放女女| 久久婷婷国产综合国色天香| 亚洲视频在线观看三级| 欧美岛国在线观看| 亚洲第一福利社区|