鏈榪戝埄鐢ㄤ竴鐐規椂闂村啓浜嗕竴涓猰atrix閿葷偧涓涓嬭嚜宸憋紝鍥犱負瀛︿範鐨勬椂鍊欓兘浼氱敤鍒幫紝鎵浠ヨ嚜宸卞氨闅忎究鍐欏啓銆備腑闂村鍒頒笉灝戙?br>姣斿錛屾渶濂囨殑鏄紝鍦ㄦ垜榪欎釜LMatrix<T>妯℃澘綾婚噷錛岀洿鎺ヤ嬌鐢╯etioflags浼氭彁紺鴻“setioflags涓嶆槸妯℃澘綾伙紝涓嶆敮鎸佹ā鏉垮弬鏁?#8221;銆?br>鍚庢潵鍦ㄤ功涓婄湅鍒幫紝鍦ㄥご鏂囦歡鍔犱笂
1 using std::setioflags
灝卞畬鍏ㄦ病鏈夐棶棰樹簡銆傜湡鏄笉寰楀叾瑙e晩錛?br> 鍙﹀錛宨ntel C++ Compiler 11 鍦ㄧ紪璇戠殑鏃跺欙紝娌℃湁浠諱綍闂錛屼絾g++ 4.4.1(鍦╱buntu 9.10 涓嬶級鍗存湁榪欎箞涓彁紺猴細
1 template<typename T>
2 const LMatrix<T>::Matrix2p LMatrix<T>::lu_resolve() const
璇村湪”<"涔嬪墠瑕佸垵濮嬪寲LMatrix<T>錛岃孧atrix2p鍙槸LMatrix<T>閲岀殑涓涓祵濂楃被銆傛垜嫻嬭瘯浜嗚繖涓嚱鏁幫紝瀹屽叏姝e父錛屽彧鏄痝++ 涓嶈兘緙栬瘧閫氳繃銆傛湡寰呴珮浜鴻В鍐充箣銆?br> 鐢變簬intel榪樹笉鏀寔鍙沖煎紩鐢紝鎵浠ヤ唬鐮侀噷鐨勬椂楂﹀嚱鏁板瓙灝卞彧濂藉鐞嗘帀浜嗐?br> 鐪嬩簡銆奺fficient c++銆嬶紝鏈潵鎯蟲妸瀹冪殑鍥哄畾澶у皬鐨勫唴瀛樻睜鎷挎潵鐩存帴鐢紝緇撴灉鍙戠幇鏈鍚庨噴鏀懼唴瀛樻睜鐨勬椂鍊欙紝宕╂簝浜嗐備唬鐮佸涓嬶細
1 template<typename T>
2 class LMatrix
3 {
4 public:
5 //

6 // other functions
7
8 ///////////overload operator new and delete to use the memorypool////////////////
9 inline void* operator new(size_t size)
10 {
11 return memPool->alloc(size);
12 }
13
14 inline void operator delete(void* element, size_t size)
15 {
16 memPool->free(element);
17 }
18
19 static void newMemoryPool()
20 {
21 memPool = new ByteMemoryPool;
22 }
23
24 static void deleteMemoryPool()
25 {
26 delete memPool;
27 }
28 ////////////////////////////////////
29
30 private:
31 static ByteMemoryPool* memPool;
32 }
鍐呭瓨姹燣MemoryPool.hpp鐨勪唬鐮佸師鏍峰紩鑷奺fficient C++銆嬶細
1 #ifndef L_MEMORYPOOL_H_
2 #define L_MEMORYPOOL_H_
3 #include "LMatrix.hpp"
4
5 template<typename T>
6 class LMemoryPool
7 {
8 public:
9 LMemoryPool(size_t size = EXPANSION_SIZE);
10 ~LMemoryPool();
11 inline void* alloc(size_t size);
12 inline void free(void* element);
13 private:
14 LMemoryPool<T>* next;
15 static const size_t EXPANSION_SIZE = 32;
16 void expandList(size_t listLength = EXPANSION_SIZE);
17 };
18
19 template<typename T>
20 LMemoryPool<T>::LMemoryPool(size_t size)
21 {
22 expandList(size);
23 }
24
25 template<typename T>
26 LMemoryPool<T>::~LMemoryPool()
27 {
28 LMemoryPool<T>* pNext = next;
29 for(pNext = next; pNext != NULL; pNext = next)
30 {
31 next = next->next;
32 delete[] pNext;
33 }
34 }
35
36 template<typename T>
37 void* LMemoryPool<T>::alloc(size_t size)
38 {
39 if(next != 0)
40 expandList();
41 LMemoryPool<T>* head = next;
42 next = head->next;
43 return head;
44 }
45
46 template<typename T>
47 void LMemoryPool<T>::free(void* element)
48 {
49 LMemoryPool<T>* head = static_cast< LMemoryPool<T>* >(element);
50 head->next = next;
51 next = head;
52 }
53
54 template<typename T>
55 void LMemoryPool<T>::expandList(size_t listLength)
56 {
57 size_t size = (sizeof(T) > sizeof(LMemoryPool<T>*))
58 ? sizeof(T) : sizeof(LMemoryPool<T>*);
59 LMemoryPool<T>* itr = reinterpret_cast< LMemoryPool<T>* >(new char[size]);
60
61 next = itr;
62
63 for(size_t i=0; i<listLength; ++i)
64 {
65 itr->next = reinterpret_cast< LMemoryPool<T>* >(new char[size]);
66 itr = itr->next;
67 }
68 itr->next = NULL;
69
70 }
71 #endif /*L_MEMORYPOOL_H_ */
嫻嬭瘯鐨則estMatrix.cpp鐢ㄤ緥濡備笅錛?br>
1 #include "LMatrix.hpp"
2 LMemoryPool< LMatrix<int> >* LMatrix<int>::memPool = 0;
3 int main( )
4 {
5 const int size = 4;
6 LMatrix<int>* array[size];
7 LMatrix<int>::newMemoryPool();
8 for(int j=0; j<size / 2; ++j)
9 {
10 for(int i=0; i<size; ++i)
11 array[i] = new LMatrix<int>(8, 8);
12
13 for(int k=0; k<size; ++k)
14 delete array[k];
15 }
16
17 LMatrix<int>::deleteMemoryPool();
18
19 return 0;
20 }
21
涓鍒囨甯哥洿鍒頒笂闈㈡祴璇曠▼搴忕殑絎?7琛岋紝榪借釜鍒板唴瀛樻睜LMemoryPool.hpp鐨勬瀽鏋勫嚱鏁幫紙絎?2琛岋級錛屽彂鐜板畠绔熺劧琚嬈℃墽琛屼簡銆傝繖涓嶆槸寰堝鎬悧錛?br>綾葷殑闈欐佹垚鍛樻槸鍏ㄥ眬鍏變韓鐨勶紝榪欓噷鍙湁涓嬈¤皟鐢紝涓轟粈涔堜細閫犳垚澶氭鏋愭瀯鍛紵鏈熷緟楂樹漢銆?br>

]]>