對物件型別和呼叫方法屬性進行儲存以提公升反射效能

2021-08-22 16:46:54 字數 2059 閱讀 1635

反射的效能差是乙個公認的事實.而最耗效能的還是根據程式集獲取要呼叫的物件,而在物件裡搜尋要呼叫的方法所耗效能到不不是很多,如果物件裡的方法不是特別的多,而去可以指定相關引數提高搜尋的效率,比如

bindingflags,binder.

如果將物件的型別和方法屬性進行一下儲存,效能方法應該會得到改觀.

簡單的做了下測試:

class

program

console.writeline(

"普通:

",environment.tickcount

-ibegin);

gc.collect();

ibegin

=environment.tickcount;

for(

inti=0

;i<

1000

;i++

)console.writeline(

"儲存type:

",environment.tickcount

-ibegin);

gc.collect();

ibegin

=environment.tickcount;

for(

inti=0

;i<

1000

;i++

)console.writeline(

"儲存methodinfo:

",environment.tickcount

-ibegin);

gc.collect();

console.readline();

}static

void

test1(

string

typename)

);methodinfo.invoke(instance,

newobject);}

//儲存type

static

void

test2(

string

typename)

);methodinfo.invoke(instance,

newobject);}

//儲存methodinfo

static

void

test3(

string

typename)

);methodinfo.invoke(instance,

newobject);}

static

typegettype(

string

typename)

else

type

=typelist[typename];

return

type;

}static

methodinfogetmethod(

string

typename,

string

methodname,typetype,

params

typetypes)

else

methodinfo

=methodlist[typename

+methodname];

return

methodinfo;}}

其中 test

.testclass

類和 test1

.class1

都有乙個無參建構函式和乙個int型別的引數及2個方法:

intval=0

;public

class1(

intval)

public

string

getvalue()

",val);

}public

intadd(

inta,

intb)

為了測試,上面2個類裡都加了幾個方法(只是定義,並沒有具體的實現**).

測試了下不進行任何處理,時間都在130左右波動,對type進行儲存,時間在32左右,基本上沒什麼波幅,而對type和methodinfo進行儲存時間維持在31.

看來這樣處理下對效能的改善還是起到了作用.

Vue 例項化Vue物件 資料和方法 屬性繫結

doctype html en utf 8 title title script head 獲取方法 h1 獲取data中的值 name h1 job p 屬性繫結 website web開發 a text v bind value name 屬性繫結 繫結標籤 websitetag p div s...

day1類和例項化的物件,方法,屬性分析

class house object mood 今天又是美好的一天 task 今天我要把你追到手 definit self,name,price,house type self.name name self.price price self.house type house type def hou...

物件導向 子類呼叫父類同名屬性和方法

class master object def init self self.kongfu 古法煎餅果子配方 例項變數,屬性 def make cake self 例項方法,方法 print 古法 按照 s 製作了乙份煎餅果子.self.kongfu class school object def ...