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

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>
            亚洲丶国产丶欧美一区二区三区| 亚洲高清电影| 亚洲免费视频在线观看| 亚洲美女在线视频| 国产精品日本精品| 久久精品国产亚洲5555| 性视频1819p久久| 激情综合中文娱乐网| 国产色视频一区| 欧美一区二区网站| 久久精品国产免费| 亚洲欧洲精品一区二区三区| 亚洲国产日韩欧美在线图片 | 亚洲女爱视频在线| 亚洲视频一起| 在线观看亚洲| 亚洲理伦电影| 国产日韩亚洲欧美| 亚洲黄色精品| 国产精品视频一| 猫咪成人在线观看| 欧美日本高清一区| 久久精品伊人| 欧美激情一区二区三区 | 国产主播一区二区三区| 欧美va天堂在线| 欧美深夜影院| 久久综合福利| 国产精品精品视频| 老色鬼精品视频在线观看播放| 欧美国产日韩在线| 久久九九免费| 欧美日韩国产专区| 女人色偷偷aa久久天堂| 欧美午夜免费电影| 亚洲国产成人av| 国产一区二区三区网站| av不卡在线| 亚洲高清一区二区三区| 亚洲一区二区在线免费观看视频| 亚洲国产精品传媒在线观看 | 久久亚洲国产精品日日av夜夜| 欧美激情1区2区3区| 久久久99久久精品女同性| 欧美精品在线极品| 欧美成va人片在线观看| 国产精品稀缺呦系列在线| 亚洲破处大片| 亚洲电影免费观看高清| 西西人体一区二区| 亚洲欧美国产毛片在线| 欧美精品二区| 亚洲高清在线观看一区| 亚洲国产91| 久久精品av麻豆的观看方式| 欧美亚洲一区二区三区| 欧美日韩系列| 日韩视频免费看| 亚洲精品一区二区在线| 美乳少妇欧美精品| 欧美chengren| 在线观看视频一区二区欧美日韩| 欧美一区=区| 久久国产主播| 国产亚洲综合性久久久影院| 亚洲自拍高清| 欧美一区二区大片| 国产精品一区三区| 欧美一级午夜免费电影| 久久精视频免费在线久久完整在线看| 国产精品久久久久久妇女6080| 日韩一级黄色av| 亚洲一区二区三区四区五区黄| 欧美日韩国产bt| 一区二区三区免费在线观看| 亚洲免费视频网站| 国产欧美日韩精品一区| 久久精彩视频| 亚洲高清久久网| 亚洲最新视频在线| 欧美少妇一区二区| 午夜激情久久久| 久久免费视频在线观看| 在线精品亚洲| 欧美精品www| 亚洲一区国产精品| 久久综合色影院| 亚洲精品国产精品国自产观看浪潮| 欧美凹凸一区二区三区视频| 亚洲精品免费在线观看| 亚洲视频久久| 国产主播一区| 欧美精品www在线观看| 在线亚洲欧美视频| 久久精品天堂| 日韩一级精品视频在线观看| 国产精品久99| 蜜臀va亚洲va欧美va天堂| 最新中文字幕亚洲| 欧美亚洲自偷自偷| 亚洲国产毛片完整版| 欧美午夜精品电影| 久久精品二区三区| 亚洲日本欧美| 久久综合九色综合网站| 一本色道久久综合精品竹菊| 国产日韩av高清| 欧美精品亚洲| 久久九九免费视频| 亚洲美女在线一区| 亚洲国产91色在线| 亚洲欧洲综合另类| 久久综合精品国产一区二区三区| 欧美激情精品久久久六区热门| 宅男精品视频| 亚洲大胆在线| 国产一区二区电影在线观看 | 久久最新视频| 亚洲欧美日韩国产中文在线| 亚洲精品乱码久久久久久按摩观| 国产精品欧美经典| 欧美理论电影网| 久久久久久高潮国产精品视| 亚洲视频在线免费观看| 亚洲欧洲精品一区二区三区 | 久久成人精品视频| 99精品热视频只有精品10| 韩国三级电影一区二区| 国产精品成人v| 欧美韩国日本综合| 久久偷看各类wc女厕嘘嘘偷窃| 亚洲影院在线观看| 艳女tv在线观看国产一区| 亚洲第一主播视频| 欧美aaaaaaaa牛牛影院| 狂野欧美激情性xxxx| 久久精品国产99精品国产亚洲性色| 亚洲午夜精品国产| 亚洲色诱最新| 一区二区三区日韩精品视频| 亚洲免费成人av| 亚洲精品精选| 亚洲美女av电影| 99re6热只有精品免费观看| 亚洲第一成人在线| 亚洲国产成人不卡| 激情欧美一区| 在线成人性视频| 亚洲精品欧美极品| 亚洲靠逼com| 国产精品99久久久久久www| 一区二区三区www| 亚洲性感美女99在线| 亚洲女同在线| 久久精品人人爽| 欧美91大片| 91久久线看在观草草青青| 亚洲精品免费一二三区| 中日韩美女免费视频网址在线观看 | 亚洲第一伊人| 亚洲精品乱码久久久久久黑人| 亚洲精品乱码久久久久| a91a精品视频在线观看| 亚洲欧美精品一区| 欧美一区二区视频免费观看| 久久精品五月| 欧美高清视频在线 | 欧美一区二区三区在线看| 久久人人爽爽爽人久久久| 欧美承认网站| 欧美体内she精视频在线观看| 国产精品日日摸夜夜摸av| 国产在线观看一区| 亚洲精品看片| 性xx色xx综合久久久xx| 久久嫩草精品久久久精品一| 亚洲国产精品一区在线观看不卡| 一区二区三区免费观看| 久久成人18免费网站| 欧美精品一区二区三区高清aⅴ| 国产精品初高中精品久久| 韩国av一区| 一区二区精品在线观看| 久久精品在线| 日韩视频不卡中文| 久久爱91午夜羞羞| 欧美精品www| 国产午夜精品一区二区三区欧美| 亚洲激情国产| 午夜欧美精品| 欧美国产免费| 午夜在线成人av| 欧美精品久久久久久| 国产综合视频在线观看| 在线亚洲精品| 欧美激情一区二区三区全黄 | 免费成人激情视频| 亚洲一区二区在线看| 欧美精品三区| 亚洲第一网站免费视频|