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

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 閱讀(292) 評論(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>
            亚洲国产精品999| 亚洲成人资源| 亚洲欧美制服另类日韩| 亚洲精品自在在线观看| 欧美久久综合| 亚洲一级黄色片| 亚洲午夜视频在线| 国产麻豆91精品| 奶水喷射视频一区| 欧美精品一区在线| 亚洲免费影视| 性久久久久久久久久久久| 黄色国产精品| 亚洲人成在线观看| 国产精品盗摄久久久| 久久久91精品国产一区二区精品| 欧美中文字幕在线| 亚洲九九精品| 午夜免费在线观看精品视频| 在线播放一区| 一本色道久久综合亚洲精品高清| 国产欧美日韩综合| 亚洲国产裸拍裸体视频在线观看乱了| 欧美性猛交一区二区三区精品| 欧美中文字幕在线观看| 欧美a级一区二区| 小辣椒精品导航| 欧美大片在线看| 久久av在线| 欧美日韩国内| 美女被久久久| 国产麻豆综合| 亚洲片区在线| 在线观看中文字幕亚洲| 一本久久精品一区二区| 在线视频国产日韩| 亚洲一区视频在线| 99视频一区| 久久久欧美精品| 欧美中文字幕在线| 欧美视频日韩视频| 亚洲成人在线网站| 在线综合亚洲欧美在线视频| 韩国精品一区二区三区| 亚洲午夜一区二区| 99伊人成综合| 欧美不卡激情三级在线观看| 久久精品国产2020观看福利| 欧美色另类天堂2015| 欧美成人精品激情在线观看| 国产色婷婷国产综合在线理论片a| 亚洲精品免费网站| 亚洲精美视频| 美国成人直播| 久久国产日韩| 国产伦理一区| 亚洲女同性videos| 中文在线资源观看网站视频免费不卡 | 免费人成网站在线观看欧美高清| 国产精品毛片高清在线完整版| 亚洲级视频在线观看免费1级| 在线观看91精品国产麻豆| 欧美一区二区精美| 小处雏高清一区二区三区| 欧美偷拍另类| 亚洲桃花岛网站| 亚洲欧美日韩国产中文在线| 欧美日韩在线亚洲一区蜜芽| 夜夜嗨av一区二区三区中文字幕 | 欧美天天综合网| 99精品热6080yy久久| 亚洲婷婷在线| 国产精品欧美一区二区三区奶水 | 亚洲综合日韩| 午夜宅男欧美| 国产夜色精品一区二区av| 欧美亚洲网站| 欧美风情在线观看| 亚洲精品视频啊美女在线直播| 欧美成年人网站| 日韩视频精品在线| 午夜国产精品影院在线观看| 国产日韩欧美一区二区三区四区 | 欧美国产日本在线| 亚洲人成人一区二区在线观看 | 亚洲视频在线一区| 久久av一区二区三区亚洲| 伊人激情综合| 欧美精品久久久久久久免费观看 | 欧美一区视频在线| 激情视频一区二区三区| 欧美成人视屏| 亚洲一区二区免费| 免费不卡在线观看| 一区二区三区精品久久久| 国产精品免费看| 久久一区二区精品| 洋洋av久久久久久久一区| 久久精品国产91精品亚洲| 在线观看免费视频综合| 欧美日韩一卡二卡| 久久久xxx| 一本色道久久综合狠狠躁篇怎么玩 | 欧美一区二区三区在线观看| 好吊成人免视频| 欧美精品色网| 欧美在线观看一区| 亚洲巨乳在线| 久久久久久亚洲精品杨幂换脸 | 国产精品久久久久久久7电影| 欧美一区二区三区啪啪| 亚洲黄色一区二区三区| 久久国产免费| 亚洲一级片在线观看| 亚洲高清一区二区三区| 国产精品久久久久久久久免费樱桃| 久久国产精品毛片| 一区二区欧美亚洲| 欧美a级片网站| 久久av一区| 亚洲一区二区三区免费视频| 亚洲福利视频一区二区| 国产欧美一区二区视频| 欧美日韩黄色大片| 免费日韩成人| 久久久精品午夜少妇| 亚洲男人av电影| 一区二区三区久久网| 亚洲高清123| 免费在线播放第一区高清av| 欧美一级视频| 亚洲一区尤物| 中国av一区| 99视频精品全国免费| 亚洲国产一区在线观看| 国产一区二区观看| 国产日韩欧美亚洲| 国产情人综合久久777777| 欧美四级在线观看| 欧美日韩性生活视频| 欧美国产亚洲视频| 欧美精品日韩www.p站| 欧美电影在线观看| 欧美大秀在线观看| 欧美激情一区二区三区成人| 欧美成人在线免费观看| 免费中文日韩| 免费短视频成人日韩| 欧美大片在线看| 欧美精品在线网站| 欧美日韩中文在线观看| 欧美视频官网| 国产免费成人| 狠狠色狠狠色综合人人| 在线国产日韩| 日韩视频亚洲视频| 一个色综合导航| 午夜亚洲视频| 久久青青草原一区二区| 欧美暴力喷水在线| 亚洲区在线播放| 亚洲午夜精品一区二区| 欧美亚洲一区二区在线| 久久久久久亚洲精品不卡4k岛国| 久久亚洲色图| 欧美日韩国产另类不卡| 国产精品萝li| 亚洲福利视频一区二区| 日韩一区二区久久| 欧美一级久久| 欧美a级理论片| 一区二区三区国产在线观看| 性欧美超级视频| 蜜桃伊人久久| 国产精品久久久久久久午夜片| 国产一区二区视频在线观看| 亚洲国产精品视频一区| 亚洲欧美色婷婷| 欧美aa在线视频| 亚洲深夜福利在线| 久久米奇亚洲| 国产精品黄页免费高清在线观看| 国内一区二区三区在线视频| 9色精品在线| 久久手机精品视频| 日韩亚洲欧美在线观看| 久久精品国产2020观看福利| 欧美日韩一区二区三区免费看| 国产综合色在线| 亚洲专区在线视频| 欧美激情一区二区久久久| 亚洲免费小视频| 欧美另类在线播放| 极品中文字幕一区| 亚洲影院免费观看| 91久久精品美女高潮| 久久久精品国产免费观看同学| 欧美三级网页| 日韩小视频在线观看专区| 久久一综合视频|