i have java class represents entry in database. each field column in database.
when want update database, construct contentvalues object.
is
arraylist<t> contentvalues = new arraylist<t>() //bla bla add ten items contentvalues`
faster than
arraylist<t> = new arraylist<t>(thisclass.class.getdeclaredfields().length); //bla bla add ten items contentvalues
what absolute performance of getdeclaredfields
? hope point out how getdeclaredfields
works. know how resize of container works don't know how getdeclaredfields
works , how perform.
micro benchmark simple hashmap using same settings contentvalues (assuming class referring to), , adding 10 fields (i.e. 1 resizing operation):
benchmark mean mean error var units c.a.p.g.a.reflection.getdeclaredfields 326.405 5.989 33.961 nsec/op c.a.p.g.a.reflection.resize 247.581 7.377 51.520 nsec/op
so getdeclaredfields
slower resizing map once, difference of order of 100 nanoseconds on desktop pc.
when adding more fields thisclass
, getdeclaredfields
gets slower, favour auto-resizing approach.
code:
@generatemicrobenchmark(benchmarktype.averagetimeperop) public map getdeclaredfields() { map m = new hashmap(thisclass.class.getdeclaredfields().length, 1.0f); (int = 0; < 9; i++) { m.put(i, "a"); } return m; } @generatemicrobenchmark(benchmarktype.averagetimeperop) public map resize() { map m = new hashmap(8, 0.75f); (int = 0; < 9; i++) { m.put(i, "a"); } return m; } class thisclass { private int i, j, k; public string s, t, u; protected list l, m, n; }
Comments
Post a Comment