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

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>
            正在播放亚洲一区| 亚洲国产一二三| 亚洲欧美精品| 亚洲一区二区免费看| 国产精品视频免费观看www| 性欧美在线看片a免费观看| 欧美亚洲一区在线| 亚洲第一伊人| 亚洲精品日日夜夜| 欧美午夜在线观看| 久久狠狠亚洲综合| 欧美电影免费观看| 亚洲专区国产精品| 久久精品国产999大香线蕉| 亚洲第一精品久久忘忧草社区| 欧美成人一品| 国产精品久久久久9999高清| 久久久国产一区二区三区| 女女同性女同一区二区三区91| 一区二区三区高清| 欧美一进一出视频| 亚洲精品韩国| 午夜一区在线| 亚洲美女淫视频| 香蕉尹人综合在线观看| 亚洲韩国日本中文字幕| 亚洲无线观看| 亚洲人精品午夜| 欧美一区二区三区久久精品茉莉花 | 91久久精品美女高潮| 欧美区一区二区三区| 久久成人国产精品| 欧美理论在线播放| 久久乐国产精品| 欧美日韩人人澡狠狠躁视频| 久久精品一本| 欧美性jizz18性欧美| 免费日韩视频| 国产精品综合色区在线观看| 亚洲国产精品va在看黑人| 国产欧美精品一区| 日韩一级精品视频在线观看| 国内欧美视频一区二区| 亚洲色诱最新| 99视频一区二区三区| 久久九九免费视频| 午夜精品久久久久| 欧美日韩的一区二区| 美女脱光内衣内裤视频久久网站| 欧美日韩一区二区三区在线| 猛男gaygay欧美视频| 国产亚洲观看| 亚洲欧美日韩综合| 亚洲欧美在线看| 欧美日韩亚洲一区二区三区在线| 免费看黄裸体一级大秀欧美| 国产一区二区三区在线观看免费| 亚洲午夜视频| 亚洲午夜极品| 欧美手机在线| 一本色道久久综合一区 | 国产精品黄页免费高清在线观看| 欧美激情91| 在线观看日韩精品| 久久亚洲精品一区二区| 蜜臀久久99精品久久久久久9| 国产欧美一区二区三区国产幕精品| 亚洲天堂免费观看| 亚洲午夜精品国产| 欧美午夜宅男影院在线观看| 亚洲最新在线| 欧美中文字幕精品| 国产午夜精品全部视频在线播放 | 欧美伊人久久久久久久久影院| 午夜在线不卡| 国产一区二区三区在线观看视频| 欧美一区国产一区| 免费视频一区| 亚洲国内自拍| 欧美日韩小视频| 一区二区三区日韩精品| 午夜在线a亚洲v天堂网2018| 国产日韩欧美一区在线 | 亚洲福利视频专区| 99精品久久久| 国产精品第三页| 午夜影院日韩| 欧美激情第10页| 亚洲免费中文| 国产一区二区无遮挡| 欧美阿v一级看视频| 洋洋av久久久久久久一区| 香蕉视频成人在线观看| 精品动漫3d一区二区三区免费| 久久综合九色九九| 亚洲美女av网站| 久久久不卡网国产精品一区| 亚洲福利视频在线| 激情成人综合| 欧美日一区二区在线观看 | 一本色道久久综合一区| 欧美日韩亚洲一区三区| 亚洲欧美精品suv| 欧美国产精品一区| 欧美亚洲专区| 亚洲精品永久免费精品| 国产精品一区二区你懂得| 久久网站免费| 中国女人久久久| 欧美激情乱人伦| 先锋资源久久| 99视频精品在线| 国外成人网址| 欧美日韩在线电影| 老司机成人在线视频| 亚洲一区在线看| 亚洲破处大片| 久久综合一区二区三区| 亚洲亚洲精品在线观看 | 国产精品羞羞答答| 欧美~级网站不卡| 久久国产加勒比精品无码| 亚洲免费精品| 欧美激情一区二区三区四区| 午夜精品久久久久久99热| 亚洲三级视频| 在线观看视频亚洲| 国产综合婷婷| 国产亚洲一区在线| 国产精品综合久久久| 欧美日韩中文在线| 欧美日韩大片一区二区三区| 免费欧美网站| 鲁鲁狠狠狠7777一区二区| 午夜国产精品影院在线观看| 99精品热6080yy久久 | 久久精品人人| 久久精品国产77777蜜臀| 午夜精品一区二区三区在线播放 | 亚洲精选中文字幕| 亚洲精品一区二区三区av| 亚洲国产精品久久91精品| 玉米视频成人免费看| 一区二区亚洲精品国产| 国产主播一区二区| 国产一区再线| 国内精品视频666| 韩国欧美一区| 亚洲国产欧美在线人成| 亚洲欧洲在线看| 一本色道久久88综合亚洲精品ⅰ| 亚洲理论在线| 亚洲永久免费av| 亚洲欧美激情在线视频| 欧美在线视频一区| 久久伊人一区二区| 欧美韩国日本综合| 最新国产乱人伦偷精品免费网站| 亚洲国产精品电影| 99精品欧美一区二区蜜桃免费| 在线综合+亚洲+欧美中文字幕| 亚洲综合精品四区| 久久精品视频网| 免费成人小视频| 欧美视频一二三区| 国产欧美日韩免费| 一区精品久久| 夜夜爽夜夜爽精品视频| 亚洲女同精品视频| 久久久久久久久蜜桃| 亚洲国产欧美在线| 亚洲午夜极品| 久久男女视频| 国产精品久久91| 伊人成人在线| 亚洲欧美日韩天堂一区二区| 久久综合狠狠综合久久综合88 | 久久久噜噜噜久久久| 欧美高清在线一区二区| 日韩亚洲视频| 久久精品2019中文字幕| 欧美日韩国产首页在线观看| 国产精品视频精品| 亚洲精品久久7777| 久久爱91午夜羞羞| 亚洲人成在线免费观看| 欧美一区二区三区喷汁尤物| 欧美夫妇交换俱乐部在线观看| 国产精品日韩久久久| 亚洲精品日韩在线观看| 久久精品日韩欧美| 亚洲开发第一视频在线播放| 久久精品国产精品亚洲综合 | 久久亚洲不卡| 国产毛片精品视频| 中日韩在线视频| 欧美韩日一区二区三区| 午夜精品国产更新| 国产精品国产三级国产a| 亚洲欧洲在线一区|